Question

I have to save my FrameworkElement as very large raster image. For now I use the RenderTargetBitmap class and a BitmapEncoder, in this way:

RenderTargetBitmap bmp = new RenderTargetBitmap(ElementWidth, ElementHeight, 
     90, 96, PixelFormats.Default);

bmp.Render(MyElement);  // OutOfMemoryException here

PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));

using (var stream = File.Create(filePath))
{ encoder.Save(stream); }

Where ElementWidth and ElementHeight are large numbers (about 10000x6000). But with this solution there's a OutOfMemoryException when i try to Render my element.

There are other ways to do what I need (without causing an OutOfMemoryException)? Thanks.

Was it helpful?

Solution

In this case, you're going to have to render the FrameworkElement in tiles. The easiest way to do this would be to set the Clip property to the position/size of the tile you want to render and then use an instance of a tile-sized RenderTargetBitmap to get that piece on disk. Now you can either

  1. Leave the pieces as-is and use them in some sort of deep-zoom type tiled renderer
  2. Combine them all (note that if they didn't fit in memory to render in the first place, you can't load 'em all to combine, either) using ImageMagick's command line/large image support or do it yourself by parsing the file format and then writing the correct output file.

Of course, I don't know if option 2 is of any help because even if you do write the huge output file - how will anyone load it? :)

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top