Skip to content

Commit 4b97eb1

Browse files
authored
a couple places where ArtQRCode creates a short lived Bitmap but neve… (#695)
a couple places where ArtQRCode creates a short lived Bitmap but never disposes of it and orphans the GDI+ handle, leading to handle exhaustion in some cases.
1 parent 443d5a1 commit 4b97eb1

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

QRCoder/ArtQRCode.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,21 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
8383
if (backgroundImage != null)
8484
{
8585
if (backgroundImageStyle == BackgroundImageStyle.Fill)
86-
graphics.DrawImage(Resize(backgroundImage, size), 0, 0);
86+
{
87+
using var resizedBg = Resize(backgroundImage, size);
88+
graphics.DrawImage(resizedBg, 0, 0);
89+
}
8790
else if (backgroundImageStyle == BackgroundImageStyle.DataAreaOnly)
8891
{
8992
var bgOffset = 4 - offset;
90-
graphics.DrawImage(Resize(backgroundImage, size - (2 * bgOffset * pixelsPerModule)), 0 + (bgOffset * pixelsPerModule), (bgOffset * pixelsPerModule));
93+
using var resizedBg = Resize(backgroundImage, size - (2 * bgOffset * pixelsPerModule));
94+
graphics.DrawImage(resizedBg, 0 + (bgOffset * pixelsPerModule), (bgOffset * pixelsPerModule));
9195
}
9296
}
9397

9498

95-
var darkModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, darkBrush);
96-
var lightModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, lightBrush);
99+
using var darkModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, darkBrush);
100+
using var lightModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, lightBrush);
97101

98102
for (var x = 0; x < numModules; x += 1)
99103
{
@@ -137,7 +141,7 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
137141
private static Bitmap MakeDotPixel(int pixelsPerModule, int pixelSize, SolidBrush brush)
138142
{
139143
// draw a dot
140-
var bitmap = new Bitmap(pixelSize, pixelSize);
144+
using var bitmap = new Bitmap(pixelSize, pixelSize);
141145
using (var graphics = Graphics.FromImage(bitmap))
142146
{
143147
graphics.FillEllipse(brush, new Rectangle(0, 0, pixelSize, pixelSize));
@@ -212,7 +216,7 @@ private static Bitmap Resize(Bitmap image, int newSize)
212216
var offsetX = (newSize - scaledWidth) / 2;
213217
var offsetY = (newSize - scaledHeight) / 2;
214218

215-
var scaledImage = new Bitmap(image, new Size(scaledWidth, scaledHeight));
219+
using var scaledImage = new Bitmap(image, new Size(scaledWidth, scaledHeight));
216220

217221
var bm = new Bitmap(newSize, newSize);
218222

0 commit comments

Comments
 (0)