从控制视图获取位图图像 [英] Get a bitmap image from a Control view

查看:23
本文介绍了从控制视图获取位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想复制到剪贴板"我的 WPF 应用程序的 Control 在屏幕上绘制的内容.因此,我需要从我的控制当前显示构建一个位图图像.

I would like to "copy to clipboard" what a Control of my WPF app draws on the screen. Therefore, I need to build a bitmap image from my Control current display.

有没有简单的方法可以做到这一点?

Is there an easy way to do that ?

提前致谢.

推荐答案

我认为这并不容易...但关键组件是 RenderTargetBitmap,您可以按如下方式使用它:

I wouldn't call it easy...but the key component is the RenderTargetBitmap, which you can use as follows:

RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.ActualWidth, (int)control.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(control);

嗯,这部分很简单,现在 RTB 已将像素存储在内部……但您的下一步是将其放入有用的格式中以将其放置在剪贴板上,而弄清楚这一点可能会很麻烦……有很多与图像相关的类,它们都相互影响.

Well, that part is easy, now the RTB has the pixels stored internally...but your next step would be putting that in a useful format to place it on the clipboard, and figuring that out can be messy...there are a lot of image related classes that all interact one or another.

这是我们用来创建 System.Drawing.Image 的内容,我认为您应该可以将其放在剪贴板上.

Here's what we use to create a System.Drawing.Image, which i think you should be able to put on the clipboard.

PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
MemoryStream stream = new MemoryStream();
png.Save(stream);
Image image = Image.FromStream(stream);

System.Drawing.Image(表单图像)无法直接与 RenderTargetBitmap(WPF 类)交互,因此我们使用 MemoryStream 对其进行转换.

System.Drawing.Image (a forms image) cannot interact directly with the RenderTargetBitmap (a WPF class), so we use a MemoryStream to convert it.

这篇关于从控制视图获取位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆