Get current image path
how do I get the current path of an image in wpf?
I am trying to build a generic function that changes the image to other image by just adding a "focus" to the end of the name of the image, btu can't seem to get the current path so I can make the change
any idea?
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);
}
EDIT 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开发者_C百科_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>
thanks,
JoeYou can modify the underlying source for your Image
in your code behind like this...where i
is your defined Image
coming from XAML or defined elsewhere. In this example path
is not used; however you will make use of it when you append focus
as mention in your question.
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();
EDIT:
Based on your code try this...
string buf = ((BitmapFrame)(sender as Image).Source).BaseUri.ToString();
You can use BitmapImage
objects, and point your image.Source
to them when necessary
BitmapImage has bitmapImage.UriSource
property
精彩评论