wpf 将 RenderTargetBitmap 转换为 Bitmap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20083210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:04:43  来源:igfitidea点击:

Convert RenderTargetBitmap to Bitmap

wpfvb.netsystem.drawingrendertargetbitmap

提问by MC9000

This looks like a dup question, it is, but no one has answered the actual question(s).

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

Here goes: Basically, I'm rendering a ViewPort3D as a 2D snapshot in code, but need to convert that type RenderTargetBitmapinto the type System.Drawing.Bitmap(for further processing on the 2D side).

这是:基本上,我在代码中将 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

While I know how to save it to a file, I'd rather skip that step and convert the bmpRento a System.Drawing.Bitmaptype, but there is no method to do so.

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

回答by PocketDews

RenderedTargetBitmapis a BitmapSource, so the BmpBitmapEncodercan do the conversion for us:

RenderedTargetBitmap是 a BitmapSource,所以BmpBitmapEncoder可以为我们做转换:

This is in C#, but it should translate to VB without any problems.

这是在 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);