2

I need to store a big Visual as bitmap image using RenderTargetBitmap without obtaining an Out-Of-Memory exception. What is the best way to achieve this?

I have no problem storing a Visual the size of the screen (let say 1920x1200 pixels), but the crash occurs when the visual is very big (20000x20000 pixels).

Maybe not using RenderTargetBitmap? are there alternatives?

NOTE: The final store target is a file, either a lossy .jpg or a losseless .png.

Néstor Sánchez A.
  • 3,848
  • 10
  • 44
  • 68

2 Answers2

2

20000 x 20000 pixels = 400000000 pixels. Times 4 bytes per pixel = 1.5 Gb (uncompressed data)

That is simply too big.

I'd create multiple images and stitch them or use even smaller images and use them as tiles.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • That creates another question: Since my PC has 8GB of RAM, why the CLR did not take advantage of my RAM? By the way, thanks for your suggestion. – Néstor Sánchez A. Apr 03 '11 at 07:35
  • Depending on the OS (32 bit/64 bit) a single process gets a maximum amount of addressable memory. Regardless of that, 1.5 Gb is a huge amount so you can't really expect a decent performance. Tiling and lazy loading (loading only when needed) are key in these situations. – Emond Apr 03 '11 at 07:40
2

Have you looked at creating an XPS document? Convert WPF (XAML) Control to XPS Document Basically, you can take a visual element and dump it out into Microsoft's version of a PDF - a standard viewer is pretty commonly installed on boxes these days, and comes built-in in Win 7. The advantage of an XPS document is that it's vector based so it's just storing drawing instructions. (You can see how if you rename an XPS doc to ZIP and unzip it.)

If you have large raster type images embedded in your visuals already, this may not work, but something to try.

Community
  • 1
  • 1
J Trana
  • 2,150
  • 2
  • 20
  • 32