Home >Backend Development >C++ >How to Display a System.Drawing.Bitmap in WPF?

How to Display a System.Drawing.Bitmap in WPF?

Barbara Streisand
Barbara StreisandOriginal
2025-01-28 01:16:09955browse

How to Display a System.Drawing.Bitmap in WPF?

Integrating System.Drawing.Bitmap into WPF Applications

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn