获取当前图像路径

时间:2011-01-07 14:23:42

标签: c# wpf image

如何在wpf中获取图像的当前路径?

我正在尝试构建一个通用函数,通过在图像名称的末尾添加“焦点”将图像更改为其他图像,btu似乎无法获取当前路径,因此我可以制作变化

任何想法?

编辑:

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 个答案:

答案 0 :(得分:2)

您可以在代码中修改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();

答案 1 :(得分:0)

您可以使用BitmapImage个对象,并在必要时将image.Source指向他们

BitmapImage具有bitmapImage.UriSource属性