【问题标题】:Convert RenderTargetBitmap to Bitmap将 RenderTargetBitmap 转换为位图
【发布时间】:2023-03-22 20:33:01
【问题描述】:

这看起来像是一个重复的问题,确实如此,但没有人回答实际问题。

这里是: 基本上,我将 ViewPort3D 渲染为代码中的 2D 快照,但需要转换该类型 RenderTargetBitmap 转换为 System.Drawing.Bitmap 类型(用于在 2D 端进行进一步处理)。

Dim bmpRen As New RenderTargetBitmap(1024, 550, 96, 96, PixelFormats.Pbgra32)
bmpRen.Render(Me.vp3dTiles) 'render the viewport as 2D snapshot

虽然我知道如何将其保存到文件中,但我宁愿跳过该步骤并将bmpRen 转换为System.Drawing.Bitmap 类型,但没有办法这样做。

【问题讨论】:

  • 不要在 WPF 中使用System.Drawing 的东西。
  • @FedericoBerasategui 诸如托盘图标之类的东西仍然需要 Win32 API 的 GDI 东西。

标签: wpf vb.net system.drawing rendertargetbitmap


【解决方案1】:

RenderedTargetBitmapBitmapSource,所以BmpBitmapEncoder 可以为我们进行转换:

这是在 C# 中,但它应该可以毫无问题地转换为 VB。

RenderTargetBitmap bmpRen = new RenderTargetBitmap(1024, 550, 96, 96, PixelFormats.Pbgra32);
bmpRen.Render(vp3dTiles);

MemoryStream stream = new MemoryStream();
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmpRen));
encoder.Save(stream);

Bitmap bitmap = new Bitmap(stream);

【讨论】:

  • 如果您需要 alpha / 透明度,请使用 PngBitmapEncoderPixelFormats.Pbgra32
猜你喜欢
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多