将 RenderTargetBitmap 转换为 System.Drawing.Image
我有 3D WPF 视觉对象,我想将其传递到 Excel 单元格(通过剪贴板缓冲区)。
对于“普通”BMP 图像,它可以工作,但我不知道如何转换 RenderTargetBitmap
。
我的代码如下所示:
System.Windows.Media.Imaging.RenderTargetBitmap renderTarget = myParent.GetViewPortAsImage(DiagramSizeX, DiagramSizeY);
System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
myImage.Source = renderTarget;
System.Drawing.Bitmap pg = new System.Drawing.Bitmap(DiagramSizeX, DiagramSizeY);
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(pg);
gr.DrawImage(myImage, 0, 0);
System.Windows.Forms.Clipboard.SetDataObject(pg, true);
sheet.Paste(range);
我的问题是 gr.DrawImage
不接受 System.Windows.Controls.Image
或 System.Windows.Media.Imaging .RenderTargetBitmap
;只有一个System.Drawing.Image
。
如何将 Controls.Image.Imaging.RenderTargetBitmap
转换为 Image
,或者是否有更简单的方法?
I have 3D WPF visual that I want to pass into an Excel cell (via clipboard buffer).
With "normal" BMP images it works but I do not know how to convert a RenderTargetBitmap
.
My code looks like this:
System.Windows.Media.Imaging.RenderTargetBitmap renderTarget = myParent.GetViewPortAsImage(DiagramSizeX, DiagramSizeY);
System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
myImage.Source = renderTarget;
System.Drawing.Bitmap pg = new System.Drawing.Bitmap(DiagramSizeX, DiagramSizeY);
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(pg);
gr.DrawImage(myImage, 0, 0);
System.Windows.Forms.Clipboard.SetDataObject(pg, true);
sheet.Paste(range);
My problem is that gr.DrawImage
does not accept a System.Windows.Controls.Image
or a System.Windows.Media.Imaging.RenderTargetBitmap
; only a System.Drawing.Image
.
How do I convert the Controls.Image.Imaging.RenderTargetBitmap
into an Image
, or are there any easier ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将
RenderTargetBitmap
中的像素直接复制到新Bitmap
的像素缓冲区中。请注意,我假设您的RenderTargetBitmap
使用PixelFormats.Pbrga32
,因为使用任何其他像素格式都会从RenderTargetBitmap
的构造函数抛出异常>。You can copy the pixels from the
RenderTargetBitmap
directly into the pixel buffer of a newBitmap
. Note that I've assumed that yourRenderTargetBitmap
usesPixelFormats.Pbrga32
, as use of any other pixel format will throw an exception from the constructor ofRenderTargetBitmap
.这是我想出的解决方案
This was the solution I came up with
也许我不明白这个问题,但是你想将 RenderTargetBitmap 复制到剪贴板,你不能直接调用 SetImage 吗?:
Maybe I don't understand the question right, but you want to copy a RenderTargetBitmap to the clipboard, couldn't you just call SetImage ?: