首页 >后端开发 >C++ >如何在wpf中显示system.drawing.bitmap?

如何在wpf中显示system.drawing.bitmap?

Barbara Streisand
Barbara Streisand原创
2025-01-28 01:16:09953浏览

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

将 System.Drawing.Bitmap 集成到 WPF 应用程序中

挑战:

如何有效地将 System.Drawing.Bitmap 图像合并到使用 System.Windows.Media.Imaging.BitmapImage 的 WPF 应用程序中?

解决方案:

简化的方法利用 MemoryStream 进行转换:

<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>

此技术将位图保存到 MemoryStream,从而可以在 WPF 环境中直接访问和使用。 然后生成的 bitmapImage 即可在 WPF 控件中使用。

以上是如何在wpf中显示system.drawing.bitmap?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn