Q: The project I'm working on requires the output of draw.GetBitmap to
be at 600 DPI. Unfortunately, sometimes a document will crop up that
causes an "Out of Memory" exception.
The problem is that the dimensions of the physical page are large: the
page dimensions are 2385x1684 points. You can use page.GetPageWidth()
& page.GetPageHeight() to obtain page dimensions (in points).
So if you are rasterizing this page at 600 DPI the total required
memory just to hold the resulting System.Drawing.Bitmap would be:
2385 * 1684 * 4 * 600/72 * 600/72 > 1GB !
(Note: 1 point is 1/72 of an inch).
So the memory required to hold only the resulting image is more than 1
The general formula to calculate the memory size for the resulting
bitmap is as follows:
memory required in bytes = number of image pixels * 4 (for 32 bit RGBA
= (paper width in points) * (dpi horizontal/72) * (paper height in
points) * (dpi vertical/72) * 4;
The memory required to process an image is directly proportional to
the selected paper size so you may want to lower the DPI as the paper
One approach to decrease memory usage is to rasterize the image in
tiles or stripes (the idea is illustrated in the last code example in
PDFDraw sample project: http://www.pdftron.com/net/samplecode.html#PDFDraw).
For example, if the input page has a media box 0,0,595,842, you could
render the page at 2000 DPI in four stripes (using 210.5 point
increments along the Y axis) as using the following crop regions:
Crop 1: 0,0,595,210.5
Crop 2: 0,210.5,595,421
Crop 3: 0,421,595,631.5
Crop 4: 0,631.5,595,842
Rendering of the same image in a single pass would require more than
1.4 GB in memory.
Using tiled rasterizing you can generate arbitrarly large bitmaps.
In case you need to generate high DPI output for printing and the
selected printer supports vector data input, you may want to consider
using pdftron.PDF.PDFRasterizer.e_GDIPlus rasterizer which can be used
to send vector data to the printer.