Skip to content

Commit 8e41dba

Browse files
committed
PDFBOX-5660: dispose graphics even if exception is thrown, as suggested by Valery Bokov; closes #434
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1932896 13f79535-47bb-0310-9956-ffa450edef68
1 parent db50306 commit 8e41dba

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFRenderer.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -326,28 +326,33 @@ public BufferedImage renderImage(int pageIndex, float scale, ImageType imageType
326326

327327
// use a transparent background if the image type supports alpha
328328
Graphics2D g = image.createGraphics();
329-
if (image.getType() == BufferedImage.TYPE_INT_ARGB)
329+
try
330330
{
331-
g.setBackground(new Color(0, 0, 0, 0));
331+
if (image.getType() == BufferedImage.TYPE_INT_ARGB)
332+
{
333+
g.setBackground(new Color(0, 0, 0, 0));
334+
}
335+
else
336+
{
337+
g.setBackground(Color.WHITE);
338+
}
339+
g.clearRect(0, 0, image.getWidth(), image.getHeight());
340+
341+
transform(g, page.getRotation(), cropBox, scale, scale);
342+
343+
// the end-user may provide a custom PageDrawer
344+
RenderingHints actualRenderingHints
345+
= renderingHints == null ? createDefaultRenderingHints(g) : renderingHints;
346+
PageDrawerParameters parameters
347+
= new PageDrawerParameters(this, page, subsamplingAllowed, destination,
348+
actualRenderingHints, imageDownscalingOptimizationThreshold);
349+
PageDrawer drawer = createPageDrawer(parameters);
350+
drawer.drawPage(g, cropBox);
332351
}
333-
else
352+
finally
334353
{
335-
g.setBackground(Color.WHITE);
354+
g.dispose();
336355
}
337-
g.clearRect(0, 0, image.getWidth(), image.getHeight());
338-
339-
transform(g, page.getRotation(), cropBox, scale, scale);
340-
341-
// the end-user may provide a custom PageDrawer
342-
RenderingHints actualRenderingHints =
343-
renderingHints == null ? createDefaultRenderingHints(g) : renderingHints;
344-
PageDrawerParameters parameters =
345-
new PageDrawerParameters(this, page, subsamplingAllowed, destination,
346-
actualRenderingHints, imageDownscalingOptimizationThreshold);
347-
PageDrawer drawer = createPageDrawer(parameters);
348-
drawer.drawPage(g, cropBox);
349-
350-
g.dispose();
351356

352357
if (image.getType() != imageType.toBufferedImageType())
353358
{

0 commit comments

Comments
 (0)