2011-01-07 5 views
0

Comment obtenir le chemin actuel d'une image dans wpf? J'essaye de construire une fonction générique qui change l'image à l'autre image en ajoutant juste un "focus" à la fin du nom de l'image, btu ne peut pas sembler obtenir le chemin courant ainsi je peux faire le changementRécupère le chemin d'image actuel

une idée?

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_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> 

grâce,
Joe

+0

D'où vient cette image vient en premier lieu? –

+0

il est normalement ajouté au designer – Jonathan

+0

Comment est-il ajouté? En tant que ressource? Contenu? –

Répondre

0

Vous pouvez utiliser BitmapImage objets et pointez votre image.Source les si nécessaire

Bitm apImage a bitmapImage.UriSource propriété

+0

Salut, merci pour la réponse. Qu'est-ce que je fais est d'ajouter Image à travers l'IDE et de définir une image à travers l'onglet propriétés de l'IDE, comment puis-je le faire avec BitmapImage? – Jonathan

2

Vous pouvez modifier la source sous-jacente pour votre Image dans votre code derrière comme ça ... où i est votre défini Image provenant XAML ou définies ailleurs. Dans cet exemple, path n'est pas utilisé; Cependant, vous l'utiliserez lorsque vous ajouterez focus comme mention dans votre 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:

Sur la base de votre code essayer ...

string buf = ((BitmapFrame)(sender as Image).Source).BaseUri.ToString(); 
+0

cela me donne une erreur parce que i.Source n'est pas un BitmapImage. Il peut devenir bitmapimage si je l'ai défini à partir du code, mais l'IDE lui donne un autre type lorsque vous définissez la source à travers elle – Jonathan

+0

i.Source est une propriété ImageSource dont BitmapImage hérite; Quelle est l'erreur exacte et où? –

+0

Impossible de convertir l'objet de type 'System.Windows.Media.Imaging.BitmapFrameDecode' en type 'System.Windows.Media.Imaging.BitmapImage'. – Jonathan

Questions connexes