WPF applications usually use
class processing images. However, when dealing with the existing object, converting it into System.Windows.Media.Imaging.BitmapImage
is a very useful step. This conversion allows these images to display and operate in WPF applications. System.Drawing.Bitmap
BitmapImage
The most effective way to convert to
. The following is a detailed step:
System.Drawing.Bitmap
BitmapImage
MemoryStream
Create a object, and use the
using(MemoryStream memory = new MemoryStream()) { bitmap.Save(memory, ImageFormat.Png); memory.Position = 0; BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); }
MemoryStream
System.Drawing.Bitmap
Create a object. Save()
ImageFormat.Png
Use and Position
Set the BitmapImage
set BeginInit()
Use to end the initialization of EndInit()
. BitmapImage
BitmapImage
Objects can be used like any other WPF image resource, such as displaying it or executing the image operation in the StreamSource
control. The above is the detailed content of How Do I Efficiently Convert a System.Drawing.Bitmap to a WPF BitmapImage?. For more information, please follow other related articles on the PHP Chinese website!