【问题标题】:Converting WriteableBitmap to Bitmap in C#在 C# 中将 WriteableBitmap 转换为位图
【发布时间】:2013-06-22 06:59:41
【问题描述】:

有什么方法可以在 C# 中将 WriteableBitmap 转换为 Bitmap 吗?

【问题讨论】:

  • @GeorgeJohnston BitmapImage 位于 System.Windows.Media 命名空间中。位图位于 System.Drawing 命名空间中。两者之间转换的方法完全不同。

标签: c# .net bitmap writeablebitmap


【解决方案1】:

实际上,这很简单。下面是一些应该工作的代码。我还没有测试它,我是从头顶写的。

private System.Drawing.Bitmap BitmapFromWriteableBitmap(WriteableBitmap writeBmp)
{
  System.Drawing.Bitmap bmp;
  using (MemoryStream outStream = new MemoryStream())
  {
    BitmapEncoder enc = new BmpBitmapEncoder();
    enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
    enc.Save(outStream);
    bmp = new System.Drawing.Bitmap(outStream);
  }
  return bmp;
}

WriteableBitmap 继承自 BitmapSource,可以直接保存到流中。然后,您从此流构建位图。

【讨论】:

  • 顺便说一句,您需要用于 .Net 3.5 的 PresentationCore 和 WindowsBase。对于 .Net 4,您还需要 System.Xaml。不知道为什么会这样。我在 Asp.NET 应用程序中使用这些引用来生成用于 TOTP 身份验证的二维码。
猜你喜欢
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 2011-05-11
  • 2018-02-19
  • 1970-01-01
  • 2012-01-12
相关资源
最近更新 更多