Thursday, April 10, 2008

Wpf Image ,Working with Image Source

Hi after a long time I am back in my blog. In this post I will discuss few things about WPF image object. Recently I have been working in a project whose UI Layer is build entirely with windows presentation foundation.

Background

WPF has another way to display image. That is, using BitmapImage object. You can declare BitmapImage tag and in WPF XAML just like Image tag. But in case of BitmapImage the source property is a Uri name is UriSource. Luckily BitmapImage is the source property of Image. Here is a simple code segment of Image and BitmapImage.

ImageSource & BitmapImage

WPF has another way to display image. That is, using BitmapImage object. You can declare BitmapImage tag and in wpf xaml just like Image tag. But in case of BitmapImage the source property is a Uri name is UriSource. Luckily BitmapImage is the source property of Image. Here is a simple code segment of Image and BitmapImage

<Image Width="200" Margin="5" Grid.Column="1" Grid.Row="1" >
<Image.Source>
<BitmapImage UriSource="sampleImages/bananas.jpg" />
</Image.Source>
</Image>

Mouse hover effect

Bellow I have listed few lines of code when i change the image in mouse enter and mouse out of an image object. Of course I am using Uri to define the location of my image then using the Uri to make a new BitmapImage and finally use BitmapImage to assign Image.Source Property.


1: private void btnGoToHome_MouseEnter(object sender, MouseEventArgs e)


2: {


3: this.Cursor = Cursors.Hand;


4: Uri src = new Uri(@"C:/Images/DifferentTransaction_Icon_Active.png");


5: BitmapImage img = new BitmapImage(src);


6: btnGoToHome.Source = img;


7: }


8:


9: private void btnGoToHome_MouseLeave(object sender, MouseEventArgs e)


10: {


11: this.Cursor = Cursors.Arrow;


12: Uri src = new Uri(@"C:/Images/DifferentTransaction_Icon_Normal.png");


13: BitmapImage img = new BitmapImage(src);


14: btnGoToHome.Source = img;


15: }




That's it you are done




Reference




http://msdn.microsoft.com/en-us/library/system.windows.controls.image.aspx

No comments:

Post a Comment