Получить текущий путь к изображению - PullRequest
0 голосов
/ 07 января 2011

как мне получить текущий путь к изображению в wpf?

Я пытаюсь создать универсальную функцию, которая изменяет изображение на другое изображение, просто добавляя «фокус» в конец имени изображения, btu не может получить текущий путь, поэтому я могу сделать изменить

есть идеи?

EDIT:

private void image1_MouseEnter(object sender, MouseEventArgs e)
{
        string buf = ((BitmapImage)(sender as Image).Source).UriSource.ToString();
        string pack = buf.Substring(0, buf.LastIndexOf('.')) + "focus" + buf.Substring(buf.LastIndexOf('.'));

        SetImageFullURI(sender as Image, pack);
}

РЕДАКТИРОВАТЬ 2:

<Window x:Class="Jode_NumeroDePeito.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="493" Width="1019" UseLayoutRounding="True" Loaded="Window_Loaded">
<Grid>
    <Border BorderBrush="#FFEBEBEB" BorderThickness="1" Height="207" HorizontalAlignment="Left" Margin="16,11,0,0" Name="border1" VerticalAlignment="Top" Width="206" CornerRadius="5"></Border>
    <Image Height="38" HorizontalAlignment="Left" Margin="27,33,0,0" Name="btPastaArtes" Stretch="None" VerticalAlignment="Top" Width="261" Source="/Jode_NumeroDePeito;component/Images/botao.jpg" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="NearestNeighbor" MouseEnter="image1_MouseEnter" MouseLeave="imgEscolherArtes_MouseLeave" Cursor="Hand" />
    <Image Cursor="Hand" Height="21" HorizontalAlignment="Left" Margin="52,19,0,0" Name="image1" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/passo1.jpg" Stretch="None" VerticalAlignment="Top" Width="97" />
    <Border BorderBrush="#FFEBEBEB" BorderThickness="1" CornerRadius="5" Height="207" HorizontalAlignment="Left" Margin="16,233,0,0" Name="border2" VerticalAlignment="Top" Width="206" />
    <Image Cursor="Hand" Height="38" HorizontalAlignment="Left" Margin="27,255,0,0" Name="btCSV" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/botao.jpg" Stretch="None" VerticalAlignment="Top" Width="261" MouseEnter="image1_MouseEnter" MouseLeave="imgEscolherArtes_MouseLeave" />
    <Image Cursor="Hand" Height="21" HorizontalAlignment="Left" Margin="52,241,0,0" Name="image3" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/passo2.jpg" Stretch="None" VerticalAlignment="Top" Width="97" />
    <Border BorderBrush="#FFEBEBEB" BorderThickness="1" Height="429" HorizontalAlignment="Left" Margin="237,11,0,0" Name="border3" VerticalAlignment="Top" Width="338" AllowDrop="False" CornerRadius="5"></Border>
    <Image Cursor="Hand" Height="21" HorizontalAlignment="Left" Margin="265,19,0,0" Name="image2" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/passo3.jpg" Stretch="None" VerticalAlignment="Top" Width="97" />
    <TextBlock Height="136" HorizontalAlignment="Left" Margin="29,74,0,0" Name="txtLogArtes" Text="" VerticalAlignment="Top" Width="181" />
    <TextBlock Height="136" HorizontalAlignment="Left" Margin="29,295,0,0" Name="txtLogCSV" Text="" VerticalAlignment="Top" Width="181" />
    <Image Cursor="Hand" Height="16" HorizontalAlignment="Left" Margin="564,161,0,0" Name="lblPreset" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/configIllustrator.jpg" Stretch="None" VerticalAlignment="Top" Width="116" />
    <ComboBox Height="16" HorizontalAlignment="Left" Margin="552,172,0,0" Name="selPreset" VerticalAlignment="Top" Width="132" />
    <Image Cursor="Hand" Height="16" HorizontalAlignment="Left" Margin="729,161,0,0" Name="lblInit" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/inicio.jpg" Stretch="None" VerticalAlignment="Top" Width="116" />
    <TextBox Height="16" HorizontalAlignment="Left" Margin="686,172,0,0" Name="txtInit" VerticalAlignment="Top" Width="120" />
    <Image Cursor="Hand" Height="16" HorizontalAlignment="Left" Margin="816,161,0,0" Name="lblQnt" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/qnt.jpg" Stretch="None" VerticalAlignment="Top" Width="116" />
    <TextBox Height="16" HorizontalAlignment="Left" Margin="808,172,0,0" Name="txtQnt" VerticalAlignment="Top" Width="120" />
    <Border BorderBrush="#FFEBEBEB" BorderThickness="1" CornerRadius="5" Height="45" HorizontalAlignment="Left" Margin="544,153,0,0" Name="pnGeneric" VerticalAlignment="Top" Width="429" Visibility="Hidden" />
    <Image Cursor="Hand" Height="30" HorizontalAlignment="Left" Margin="930,163,0,0" Name="imgFechar" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/x.jpg" Stretch="None" VerticalAlignment="Top" Width="35" />
    <Image Cursor="Hand" Height="38" HorizontalAlignment="Left" Margin="249,33,0,0" Name="btAdd" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/adic.jpg" Stretch="None" VerticalAlignment="Top" Width="261" />
    <Border BorderBrush="Silver" BorderThickness="0" Height="357" HorizontalAlignment="Left" Margin="242,75,0,0" Name="pnContainer" VerticalAlignment="Top" Width="329" />
    <Image Cursor="Hand" Height="21" HorizontalAlignment="Left" Margin="611,19,0,0" Name="image4" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/passo3.jpg" Stretch="None" VerticalAlignment="Top" Width="97" />
    <Image Cursor="Hand" Height="38" HorizontalAlignment="Left" Margin="595,33,0,0" Name="btImprimir" RenderOptions.BitmapScalingMode="NearestNeighbor" SnapsToDevicePixels="True" Source="/Jode_NumeroDePeito;component/Images/adic.jpg" Stretch="None" VerticalAlignment="Top" Width="261" />
    <TextBlock Height="168" Margin="636,241,210,0" Name="txtLog" Text="TextBlock" VerticalAlignment="Top" />
</Grid>
</Window>

спасибо,
Джо

Ответы [ 2 ]

2 голосов
/ 07 января 2011

Вы можете изменить исходный код для вашего Image в вашем коде, например так ... где i - это ваш определенный Image, полученный из XAML или определенный где-то еще. В этом примере path не используется; однако вы будете использовать его, когда добавите focus в качестве упоминания в своем вопросе.

Image i = new Image();

BitmapImage bi = i.Source as BitmapImage;
bi.BeginInit();

Uri uri = bi.UriSource;
String path = uri.AbsolutePath;
String newPath = @"C:\Images\blah.png";
Uri newUri = new Uri(newPath);

bi.UriSource = newUri;
bi.EndInit();

РЕДАКТИРОВАТЬ:

На основе вашего кода попробуйте это ...

string buf = ((BitmapFrame)(sender as Image).Source).BaseUri.ToString();
0 голосов
/ 07 января 2011

Вы можете использовать BitmapImage объекты и указывать им image.Source при необходимости

BitmapImage имеет bitmapImage.UriSource свойство

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...