Skip to content

Commit ff31b5f

Browse files
LakritzatorJimBobSquarePants
authored andcommitted
Some changes to make the usages of GDI in benchmarks and tests more stable, this should hopefully prevent "a generic error occurred in GDI+". Hopefully I didn't miss one... (#835)
1 parent 4c0b012 commit ff31b5f

File tree

7 files changed

+104
-78
lines changed

7 files changed

+104
-78
lines changed

tests/ImageSharp.Benchmarks/Drawing/DrawBeziers.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ public void DrawPathSystemDrawing()
2626
{
2727
graphics.InterpolationMode = InterpolationMode.Default;
2828
graphics.SmoothingMode = SmoothingMode.AntiAlias;
29-
Pen pen = new Pen(System.Drawing.Color.HotPink, 10);
30-
graphics.DrawBeziers(pen, new[] {
31-
new PointF(10, 500),
32-
new PointF(30, 10),
33-
new PointF(240, 30),
34-
new PointF(300, 500)
35-
});
29+
using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
30+
{
31+
graphics.DrawBeziers(pen, new[] {
32+
new PointF(10, 500),
33+
new PointF(30, 10),
34+
new PointF(240, 30),
35+
new PointF(300, 500)
36+
});
37+
}
3638
}
3739

3840
using (MemoryStream ms = new MemoryStream())

tests/ImageSharp.Benchmarks/Drawing/DrawLines.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ public void DrawPathSystemDrawing()
2626
{
2727
graphics.InterpolationMode = InterpolationMode.Default;
2828
graphics.SmoothingMode = SmoothingMode.AntiAlias;
29-
var pen = new Pen(System.Drawing.Color.HotPink, 10);
30-
graphics.DrawLines(pen, new[] {
31-
new PointF(10, 10),
32-
new PointF(550, 50),
33-
new PointF(200, 400)
34-
});
29+
using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
30+
{
31+
graphics.DrawLines(pen, new[] {
32+
new PointF(10, 10),
33+
new PointF(550, 50),
34+
new PointF(200, 400)
35+
});
36+
}
3537
}
3638

3739
using (var ms = new MemoryStream())

tests/ImageSharp.Benchmarks/Drawing/DrawPolygon.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ public void DrawPolygonSystemDrawing()
2626
{
2727
graphics.InterpolationMode = InterpolationMode.Default;
2828
graphics.SmoothingMode = SmoothingMode.AntiAlias;
29-
Pen pen = new Pen(System.Drawing.Color.HotPink, 10);
30-
graphics.DrawPolygon(pen, new[] {
31-
new PointF(10, 10),
32-
new PointF(550, 50),
33-
new PointF(200, 400)
34-
});
29+
using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
30+
{
31+
graphics.DrawPolygon(pen, new[] {
32+
new PointF(10, 10),
33+
new PointF(550, 50),
34+
new PointF(200, 400)
35+
});
36+
}
3537
}
3638

3739
using (MemoryStream ms = new MemoryStream())

tests/ImageSharp.Benchmarks/Drawing/DrawText.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public void DrawTextSystemDrawing()
3535
{
3636
graphics.InterpolationMode = InterpolationMode.Default;
3737
graphics.SmoothingMode = SmoothingMode.AntiAlias;
38-
Pen pen = new Pen(System.Drawing.Color.HotPink, 10);
39-
var font = new Font("Arial", 12, GraphicsUnit.Point);
40-
graphics.DrawString(TextToRender, font, System.Drawing.Brushes.HotPink, new RectangleF(10, 10, 780, 780));
38+
using (var font = new Font("Arial", 12, GraphicsUnit.Point))
39+
{
40+
graphics.DrawString(TextToRender, font, System.Drawing.Brushes.HotPink, new RectangleF(10, 10, 780, 780));
41+
}
4142
}
4243
}
4344
}

tests/ImageSharp.Benchmarks/Drawing/DrawTextOutline.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ public void DrawTextSystemDrawing()
3434
{
3535
graphics.InterpolationMode = InterpolationMode.Default;
3636
graphics.SmoothingMode = SmoothingMode.AntiAlias;
37-
Pen pen = new Pen(System.Drawing.Color.HotPink, 10);
38-
var font = new Font("Arial", 12, GraphicsUnit.Point);
39-
var gp = new GraphicsPath();
40-
gp.AddString(TextToRender, font.FontFamily, (int)font.Style, font.Size, new RectangleF(10, 10, 780, 780), new StringFormat());
41-
graphics.DrawPath(pen, gp);
37+
using (var pen = new Pen(System.Drawing.Color.HotPink, 10))
38+
using (var font = new Font("Arial", 12, GraphicsUnit.Point))
39+
using (var gp = new GraphicsPath())
40+
{
41+
gp.AddString(TextToRender, font.FontFamily, (int)font.Style, font.Size, new RectangleF(10, 10, 780, 780), new StringFormat());
42+
graphics.DrawPath(pen, gp);
43+
}
4244
}
4345
}
4446
}

tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ public void DrawPatternPolygonSystemDrawing()
2626
using (Graphics graphics = Graphics.FromImage(destination))
2727
{
2828
graphics.SmoothingMode = SmoothingMode.AntiAlias;
29-
HatchBrush brush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.HotPink);
30-
graphics.FillRectangle(brush, new Rectangle(0, 0, 800, 800)); // can't find a way to flood fill with a brush
29+
using (var brush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.HotPink))
30+
{
31+
graphics.FillRectangle(brush, new Rectangle(0, 0, 800, 800)); // can't find a way to flood fill with a brush
32+
}
3133
}
3234
using (MemoryStream ms = new MemoryStream())
3335
{

tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,39 @@ internal static unsafe Image<TPixel> From32bppArgbSystemDrawingBitmap<TPixel>(Bi
3939
}
4040

4141
BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
42-
byte* sourcePtrBase = (byte*)data.Scan0;
42+
var image = new Image<TPixel>(w, h);
43+
try
44+
{
45+
byte* sourcePtrBase = (byte*)data.Scan0;
4346

44-
long sourceRowByteCount = data.Stride;
45-
long destRowByteCount = w * sizeof(Bgra32);
47+
long sourceRowByteCount = data.Stride;
48+
long destRowByteCount = w * sizeof(Bgra32);
4649

47-
var image = new Image<TPixel>(w, h);
48-
Configuration configuration = image.GetConfiguration();
50+
Configuration configuration = image.GetConfiguration();
4951

50-
using (IMemoryOwner<Bgra32> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgra32>(w))
51-
{
52-
fixed (Bgra32* destPtr = &workBuffer.GetReference())
52+
using (IMemoryOwner<Bgra32> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgra32>(w))
5353
{
54-
for (int y = 0; y < h; y++)
54+
fixed (Bgra32* destPtr = &workBuffer.GetReference())
5555
{
56-
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
57-
58-
byte* sourcePtr = sourcePtrBase + (data.Stride * y);
59-
60-
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
61-
PixelOperations<TPixel>.Instance.FromBgra32(
62-
configuration,
63-
workBuffer.GetSpan().Slice(0, w),
64-
row);
56+
for (int y = 0; y < h; y++)
57+
{
58+
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
59+
60+
byte* sourcePtr = sourcePtrBase + (data.Stride * y);
61+
62+
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
63+
PixelOperations<TPixel>.Instance.FromBgra32(
64+
configuration,
65+
workBuffer.GetSpan().Slice(0, w),
66+
row);
67+
}
6568
}
6669
}
6770
}
71+
finally
72+
{
73+
bmp.UnlockBits(data);
74+
}
6875

6976
return image;
7077
}
@@ -91,33 +98,36 @@ internal static unsafe Image<TPixel> From24bppRgbSystemDrawingBitmap<TPixel>(Bit
9198
}
9299

93100
BitmapData data = bmp.LockBits(fullRect, ImageLockMode.ReadWrite, bmp.PixelFormat);
94-
byte* sourcePtrBase = (byte*)data.Scan0;
101+
var image = new Image<TPixel>(w, h);
102+
try
103+
{
104+
byte* sourcePtrBase = (byte*)data.Scan0;
95105

96-
long sourceRowByteCount = data.Stride;
97-
long destRowByteCount = w * sizeof(Bgr24);
106+
long sourceRowByteCount = data.Stride;
107+
long destRowByteCount = w * sizeof(Bgr24);
98108

99-
var image = new Image<TPixel>(w, h);
100-
Configuration configuration = image.GetConfiguration();
109+
Configuration configuration = image.GetConfiguration();
101110

102-
using (IMemoryOwner<Bgr24> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgr24>(w))
103-
{
104-
fixed (Bgr24* destPtr = &workBuffer.GetReference())
111+
using (IMemoryOwner<Bgr24> workBuffer = Configuration.Default.MemoryAllocator.Allocate<Bgr24>(w))
105112
{
106-
for (int y = 0; y < h; y++)
113+
fixed (Bgr24* destPtr = &workBuffer.GetReference())
107114
{
108-
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
115+
for (int y = 0; y < h; y++)
116+
{
117+
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
109118

110-
byte* sourcePtr = sourcePtrBase + (data.Stride * y);
119+
byte* sourcePtr = sourcePtrBase + (data.Stride * y);
111120

112-
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
113-
PixelOperations<TPixel>.Instance.FromBgr24(
114-
configuration,
115-
workBuffer.GetSpan().Slice(0, w),
116-
row);
121+
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
122+
PixelOperations<TPixel>.Instance.FromBgr24(configuration, workBuffer.GetSpan().Slice(0, w), row);
123+
}
117124
}
118125
}
119126
}
120-
127+
finally
128+
{
129+
bmp.UnlockBits(data);
130+
}
121131
return image;
122132
}
123133

@@ -131,27 +141,32 @@ internal static unsafe Bitmap To32bppArgbSystemDrawingBitmap<TPixel>(Image<TPixe
131141
var resultBitmap = new Bitmap(w, h, PixelFormat.Format32bppArgb);
132142
var fullRect = new Rectangle(0, 0, w, h);
133143
BitmapData data = resultBitmap.LockBits(fullRect, ImageLockMode.ReadWrite, resultBitmap.PixelFormat);
134-
byte* destPtrBase = (byte*)data.Scan0;
144+
try
145+
{
146+
byte* destPtrBase = (byte*)data.Scan0;
135147

136-
long destRowByteCount = data.Stride;
137-
long sourceRowByteCount = w * sizeof(Bgra32);
148+
long destRowByteCount = data.Stride;
149+
long sourceRowByteCount = w * sizeof(Bgra32);
138150

139-
using (IMemoryOwner<Bgra32> workBuffer = image.GetConfiguration().MemoryAllocator.Allocate<Bgra32>(w))
140-
{
141-
fixed (Bgra32* sourcePtr = &workBuffer.GetReference())
151+
using (IMemoryOwner<Bgra32> workBuffer = image.GetConfiguration().MemoryAllocator.Allocate<Bgra32>(w))
142152
{
143-
for (int y = 0; y < h; y++)
153+
fixed (Bgra32* sourcePtr = &workBuffer.GetReference())
144154
{
145-
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
146-
PixelOperations<TPixel>.Instance.ToBgra32(configuration, row, workBuffer.GetSpan());
147-
byte* destPtr = destPtrBase + (data.Stride * y);
148-
149-
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
155+
for (int y = 0; y < h; y++)
156+
{
157+
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
158+
PixelOperations<TPixel>.Instance.ToBgra32(configuration, row, workBuffer.GetSpan());
159+
byte* destPtr = destPtrBase + (data.Stride * y);
160+
161+
Buffer.MemoryCopy(sourcePtr, destPtr, destRowByteCount, sourceRowByteCount);
162+
}
150163
}
151164
}
152165
}
153-
154-
resultBitmap.UnlockBits(data);
166+
finally
167+
{
168+
resultBitmap.UnlockBits(data);
169+
}
155170

156171
return resultBitmap;
157172
}

0 commit comments

Comments
 (0)