Development/C#
Image Source 변경
@위너스
2012. 2. 7. 16:00
WPF에서 마우스 오버시 이미지를 변경하기 위한 Image Source 변경.
1. 첫번째 방법(Code Behind)
var uriSource = new Uri(@"/WpfApplication1;component/changeimage.png", UriKind.Relative);
ImageName.Source = new BitmapImage(uriSource);
ImageName.Source = new BitmapImage(uriSource);
2. 두번째 방법(Code Behind)
string uriSource = "pack://application:,,,/AssemblyName;component/changeimage.png";
ImageName.Source = new ImageSourceConverter().ConvertFromString(uriSource) as ImageSource;
ImageName.Source = new ImageSourceConverter().ConvertFromString(uriSource) as ImageSource;
3. 세번째 방법(Code Behind)
Assembly asm = Assembly.GetExecutingAssembly();
Stream iconStream = asm.GetManifestResourceStream("changeimage.png");
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = iconStream;
bitmap.EndInit();
ImageName.Source = bitmap;
Stream iconStream = asm.GetManifestResourceStream("changeimage.png");
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = iconStream;
bitmap.EndInit();
ImageName.Source = bitmap;
4.xaml 코드에서의 변경
<Image x:Name="img">
<Image.Style>
<Style>
<Setter Property="Image.Source" Value="/WpfApplication1;component/original_image.png" />
<Style.Triggers>
<Trigger Property="Image.IsMouseOver" Value="True">
<Setter Property="Image.Source" Value="/WpfApplication1;component/change_image.png"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Image.Style>
<Style>
<Setter Property="Image.Source" Value="/WpfApplication1;component/original_image.png" />
<Style.Triggers>
<Trigger Property="Image.IsMouseOver" Value="True">
<Setter Property="Image.Source" Value="/WpfApplication1;component/change_image.png"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>