tags:

views:

51

answers:

2

I'm rendering a WPF canvas to an image and sticking it on the clipboard.

If the canvas is small (< 900px square) it all works fine.

I have a larger canvas (3000+px square) and the clipboard is empty (paste option disabled in photoshop/word etc)

var transform = canvas.LayoutTransform;
canvas.LayoutTransform = null;

var size = new Size(canvas.Width, canvas.Height);

canvas.Measure(size);
canvas.Arrange(new Rect(size));

var renderBitmap = new RenderTargetBitmap((int) size.Width, (int) size.Height, 96d, 96d, PixelFormats.Pbgra32);
renderBitmap.Render(canvas);

canvas.LayoutTransform = transform;

Clipboard.SetImage(renderBitmap);

I've not found if there is a threshold size that causes this to break.

3140 x 1903 doesnt work, 3140 x 317 does

Whats going on?

Thanks

A: 

It'll be a size limit issue you are hitting.

This link may be helpful:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/f8ba29c4-1c1c-4fd6-bfc0-68a07930c74f

Sarkie
+2  A: 

It turns out that in order to store an image to the clipboard, the image is automatically converted into uncompressed bitmaps in several formats (BMP, DIB, etc.). So when you have a 10MP image that takes up 40MB uncompressed (8-bit RGBA), it could take 200MB of memory to store on the clipboard -- just in case somebody might want it in one of those other formats.

What you might be able to do is put it on the clipboard yourself without as much overhead. If you use Reflector, you'll see that Clipboard.SetImage looks like this:

public static void SetImage(Image image)
{
    if (image == null)
    {
        throw new ArgumentNullException("image");
    }
    IDataObject data = new DataObject();
    data.SetData(DataFormats.Bitmap, true, image); // true means autoconvert
    Clipboard.SetDataObject(data, true); // true means copy
}

If you make your own version of the SetImage function with one or both of the true instances set to false, you may be able to overcome some of the unnecessary copying and be able to put a larger image on the clipboard.

Gabe