Skip to content

Commit b930912

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/branches/2.0@1932894 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7467d49 commit b930912

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
@@ -351,28 +351,33 @@ public BufferedImage renderImage(int pageIndex, float scale, ImageType imageType
351351

352352
// use a transparent background if the image type supports alpha
353353
Graphics2D g = image.createGraphics();
354-
if (image.getType() == BufferedImage.TYPE_INT_ARGB)
354+
try
355355
{
356-
g.setBackground(new Color(0, 0, 0, 0));
356+
if (image.getType() == BufferedImage.TYPE_INT_ARGB)
357+
{
358+
g.setBackground(new Color(0, 0, 0, 0));
359+
}
360+
else
361+
{
362+
g.setBackground(Color.WHITE);
363+
}
364+
g.clearRect(0, 0, image.getWidth(), image.getHeight());
365+
366+
transform(g, page.getRotation(), cropBox, scale, scale);
367+
368+
// the end-user may provide a custom PageDrawer
369+
RenderingHints actualRenderingHints =
370+
renderingHints == null ? createDefaultRenderingHints(g) : renderingHints;
371+
PageDrawerParameters parameters =
372+
new PageDrawerParameters(this, page, subsamplingAllowed, destination,
373+
actualRenderingHints, imageDownscalingOptimizationThreshold);
374+
PageDrawer drawer = createPageDrawer(parameters);
375+
drawer.drawPage(g, cropBox);
357376
}
358-
else
377+
finally
359378
{
360-
g.setBackground(Color.WHITE);
379+
g.dispose();
361380
}
362-
g.clearRect(0, 0, image.getWidth(), image.getHeight());
363-
364-
transform(g, page.getRotation(), cropBox, scale, scale);
365-
366-
// the end-user may provide a custom PageDrawer
367-
RenderingHints actualRenderingHints =
368-
renderingHints == null ? createDefaultRenderingHints(g) : renderingHints;
369-
PageDrawerParameters parameters =
370-
new PageDrawerParameters(this, page, subsamplingAllowed, destination,
371-
actualRenderingHints, imageDownscalingOptimizationThreshold);
372-
PageDrawer drawer = createPageDrawer(parameters);
373-
drawer.drawPage(g, cropBox);
374-
375-
g.dispose();
376381

377382
if (image.getType() != imageType.toBufferedImageType())
378383
{

0 commit comments

Comments
 (0)