Challenge:
How do you effectively incorporate a System.Drawing.Bitmap image into a WPF application, which uses System.Windows.Media.Imaging.BitmapImage?
Solution:
A streamlined approach leverages a MemoryStream for the conversion:
<code class="language-csharp">using System.IO; using System.Windows.Media.Imaging; using (MemoryStream memoryStream = new MemoryStream()) { bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); memoryStream.Position = 0; BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memoryStream; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); }</code>
This technique saves the Bitmap to a MemoryStream, enabling straightforward access and usage within the WPF environment. The resulting bitmapImage
is then ready for use in WPF controls.
The above is the detailed content of How to Display a System.Drawing.Bitmap in WPF?. For more information, please follow other related articles on the PHP Chinese website!