The source property of the WPF image control is a ImageSource object

Source: Internet
Author: User

1. The fixed picture path is possible, as follows:

<image source= ". /test.png "/>

2. The bound path is a string-type picture path, and in fact it requires a imagesource, so simply mark it as a path type value in front of it OK!

<DataTemplate>
<image source= "{Binding path= IconPath}"/> </DataTemplate>

--------------------------------------------------------------------------------------------------------------- -------------------

Many times, we use images to decorate our UI, such as the background of a control.

These images can be divided into two forms: images that exist in the local file system and images that exist in memory

For these two forms of pictures, in WPF, the use of different methods, the following main explanation for the two forms of the use of the image

A picture file that exists in the local file system
For such pictures, it is very simple to specify the path directly in XAML, such as:
<Button>
<Button.Background>
<imagebrush imagesource= "Bg.jpg"/>
</Button.Background>
</Button>
The corresponding C # code is
ImageBrush ImageBrush = new ImageBrush ();
Imagebrush.imagesource = new BitmapImage (New Uri ("Bg.jpg", urikind.relative));
button. Background = ImageBrush;
Where the type of Imagebrush.imagesource is ImageSource, and ImageSource is an abstract class,
So we can't use it directly, instead of using its subclasses, and looking at MSDN, we can see the inheritance relationship:
System.Windows.Media.ImageSource
System.Windows.Media.DrawingImage
System.Windows.Media.Imaging.BitmapSource

Second, in-memory images
For images that exist only in memory, there is nothing to do with the above method, we should find another method, here is a way:
Look at the code first:

Here the picture is read from the file to simulate the image in memory
System.Drawing.Bitmap Bitmap = new System.Drawing.Bitmap ("bg.jpg");
MemoryStream stream = new MemoryStream ();
Bitmap. Save (stream, System.Drawing.Imaging.ImageFormat.Png);
ImageBrush ImageBrush = new ImageBrush ();
Imagesourceconverter imagesourceconverter = new Imagesourceconverter ();
button. Background = ImageBrush;
Where bitmap is a picture of the bitmap type that exists in memory, which is simulated using the direct load local image file.
The step is to save it to the stream first, and then use the ConvertFrom method of the Imagesourceconverter class to get the picture we need from the stream.
OK, this article to the end, the above methods are their own in use to explore the gains, if there is a better way, I am very willing to communicate with you.

--------------------------------------------------------------------------------------------------------------- ------------------

<StackPanel>
<StackPanel.Resources>
<local:imageconverter x:key= "Cvt_image"/>
</StackPanel.Resources>
<textbox x:name= "Txtpath" text= "E:\.....\aaa.jpg"/>
<image source= "{Binding elementname=txtpath, path=text}"/>
<image source= "{Binding elementname=txtpath, Path=text, Converter={staticresource cvt_image}"/>
</StackPanel>

Here is just a demonstration, in fact, the database can be taken out of the binary data, directly converted to a BitmapImage object returned:
public class Imageconverter:ivalueconverter
{
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new BitmapImage (The new Uri (value). ToString ()));
}

public object Convertback (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
}

The source of the picture is the ImageSource type, and the class inheriting the type has: BitmapImage (inherited from BitmapSource) and Drawingimage (inherited from ImageSource). BitmapImage can display a normal bitmap, drawingimage can display vector images

The source property of the WPF image control is a ImageSource object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.