tags:

views:

150

answers:

1

Hello,

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 ?

Thanks in advance.

+1  A: 

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);

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.

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 (a forms image) cannot interact directly with the RenderTargetBitmap (a WPF class), so we use a MemoryStream to convert it.

Bubblewrap
Thank you, this was my answer.The nice thing is that Clipboard.SetImage() requires a BitmapSource, so I can directly give it the RenderTargetBitmap. Works like a charm. You can call it easy. Thanks again !Now I need to figure out how to allow the user to manipulate the control visual (zooming it essentially) before exporting it to a fixed-resolution bitmap. i think I will copy the current display as a VisualBrush in a Viewbox, and let the user plays with it before exporting.
Aurélien Ribon