diff --git a/src/Barcoder.Renderer.Image/Barcoder.Renderer.Image.csproj b/src/Barcoder.Renderer.Image/Barcoder.Renderer.Image.csproj
index 6790125..971afe6 100644
--- a/src/Barcoder.Renderer.Image/Barcoder.Renderer.Image.csproj
+++ b/src/Barcoder.Renderer.Image/Barcoder.Renderer.Image.csproj
@@ -16,7 +16,7 @@
-
+
diff --git a/src/Barcoder.Renderer.Image/ImageRenderer.cs b/src/Barcoder.Renderer.Image/ImageRenderer.cs
index c280546..65d2af8 100644
--- a/src/Barcoder.Renderer.Image/ImageRenderer.cs
+++ b/src/Barcoder.Renderer.Image/ImageRenderer.cs
@@ -50,72 +50,89 @@ public void Render(IBarcode barcode, Stream outputStream)
barcode = barcode ?? throw new ArgumentNullException(nameof(barcode));
outputStream = outputStream ?? throw new ArgumentNullException(nameof(outputStream));
if (barcode.Bounds.Y == 1)
- Render1D(barcode, outputStream);
+ {
+ using (var image = Render1D(barcode))
+ {
+ image.Save(outputStream, _imageEncoder.Value);
+ }
+ }
+ else if (barcode.Bounds.Y > 1)
+ {
+ using (var image = Render2D(barcode))
+ {
+ image.Save(outputStream, _imageEncoder.Value);
+ }
+ }
+ else
+ throw new NotSupportedException($"Y value of {barcode.Bounds.Y} is invalid");
+ }
+
+ public Image Render(IBarcode barcode)
+ {
+ barcode = barcode ?? throw new ArgumentNullException(nameof(barcode));
+ if (barcode.Bounds.Y == 1)
+ return Render1D(barcode);
else if (barcode.Bounds.Y > 1)
- Render2D(barcode, outputStream);
+ return Render2D(barcode);
else
throw new NotSupportedException($"Y value of {barcode.Bounds.Y} is invalid");
}
- private void Render1D(IBarcode barcode, Stream outputStream)
+ private Image Render1D(IBarcode barcode)
{
int margin = _options.CustomMargin ?? barcode.Margin;
int width = (barcode.Bounds.X + 2 * margin) * _options.PixelSize;
int height = (_options.BarHeightFor1DBarcode + 2 * margin) * _options.PixelSize;
- using (var image = new Image(width, height))
+ var image = new Image(width, height);
+ image.Mutate(ctx =>
{
- image.Mutate(ctx =>
+ ctx.Fill(Color.White);
+ for (var x = 0; x < barcode.Bounds.X; x++)
{
- ctx.Fill(Color.White);
- for (var x = 0; x < barcode.Bounds.X; x++)
- {
- if (!barcode.At(x, 0))
- continue;
- ctx.FillPolygon(
- Color.Black,
- new Vector2((margin + x) * _options.PixelSize, margin * _options.PixelSize),
- new Vector2((margin + x + 1) * _options.PixelSize, margin * _options.PixelSize),
- new Vector2((margin + x + 1) * _options.PixelSize, (_options.BarHeightFor1DBarcode + margin) * _options.PixelSize),
- new Vector2((margin + x) * _options.PixelSize, (_options.BarHeightFor1DBarcode + margin) * _options.PixelSize));
- }
- });
+ if (!barcode.At(x, 0))
+ continue;
+ ctx.FillPolygon(
+ Color.Black,
+ new Vector2((margin + x) * _options.PixelSize, margin * _options.PixelSize),
+ new Vector2((margin + x + 1) * _options.PixelSize, margin * _options.PixelSize),
+ new Vector2((margin + x + 1) * _options.PixelSize, (_options.BarHeightFor1DBarcode + margin) * _options.PixelSize),
+ new Vector2((margin + x) * _options.PixelSize, (_options.BarHeightFor1DBarcode + margin) * _options.PixelSize));
+ }
+ });
- if (_options.IncludeEanContentAsText && barcode.IsEanBarcode())
- EanContentRenderer.Render(image, barcode, fontFamily: _options.EanFontFamily, scale: _options.PixelSize);
+ if (_options.IncludeEanContentAsText && barcode.IsEanBarcode())
+ EanContentRenderer.Render(image, barcode, fontFamily: _options.EanFontFamily, scale: _options.PixelSize);
- image.Save(outputStream, _imageEncoder.Value);
- }
+ return image;
}
- private void Render2D(IBarcode barcode, Stream outputStream)
+ private Image Render2D(IBarcode barcode)
{
int margin = _options.CustomMargin ?? barcode.Margin;
int width = (barcode.Bounds.X + 2 * margin) * _options.PixelSize;
int height = (barcode.Bounds.Y + 2 * margin) * _options.PixelSize;
- using (var image = new Image(width, height))
+ var image = new Image(width, height);
+ image.Mutate(ctx =>
{
- image.Mutate(ctx =>
+ ctx.Fill(Color.White);
+ for (var y = 0; y < barcode.Bounds.Y; y++)
{
- ctx.Fill(Color.White);
- for (var y = 0; y < barcode.Bounds.Y; y++)
+ for (var x = 0; x < barcode.Bounds.X; x++)
{
- for (var x = 0; x < barcode.Bounds.X; x++)
- {
- if (!barcode.At(x, y)) continue;
- ctx.FillPolygon(
- Color.Black,
- new Vector2((margin + x) * _options.PixelSize, (margin + y) * _options.PixelSize),
- new Vector2((margin + x + 1) * _options.PixelSize, (margin + y) * _options.PixelSize),
- new Vector2((margin + x + 1) * _options.PixelSize, (margin + y + 1) * _options.PixelSize),
- new Vector2((margin + x) * _options.PixelSize, (margin + y + 1) * _options.PixelSize));
- }
+ if (!barcode.At(x, y)) continue;
+ ctx.FillPolygon(
+ Color.Black,
+ new Vector2((margin + x) * _options.PixelSize, (margin + y) * _options.PixelSize),
+ new Vector2((margin + x + 1) * _options.PixelSize, (margin + y) * _options.PixelSize),
+ new Vector2((margin + x + 1) * _options.PixelSize, (margin + y + 1) * _options.PixelSize),
+ new Vector2((margin + x) * _options.PixelSize, (margin + y + 1) * _options.PixelSize));
}
- });
+ }
+ });
- image.Save(outputStream, _imageEncoder.Value);
- }
+ return image;
}
}
}
diff --git a/tests/Barcoder.Renderer.Image.Tests/Barcoder.Renderer.Image.Tests.csproj b/tests/Barcoder.Renderer.Image.Tests/Barcoder.Renderer.Image.Tests.csproj
index 46633a1..ea4dba6 100644
--- a/tests/Barcoder.Renderer.Image.Tests/Barcoder.Renderer.Image.Tests.csproj
+++ b/tests/Barcoder.Renderer.Image.Tests/Barcoder.Renderer.Image.Tests.csproj
@@ -10,7 +10,7 @@
-
+
all
diff --git a/tests/Barcoder.Renderer.Image.Tests/ImageRendererTests.cs b/tests/Barcoder.Renderer.Image.Tests/ImageRendererTests.cs
index 154438b..7b735aa 100644
--- a/tests/Barcoder.Renderer.Image.Tests/ImageRendererTests.cs
+++ b/tests/Barcoder.Renderer.Image.Tests/ImageRendererTests.cs
@@ -140,6 +140,23 @@ public void Render_ImageFormatPng_ShouldRenderPng()
imageFormat.Name.Should().Be("PNG");
}
+ [Fact]
+ public void Render_Image_ShouldRender()
+ {
+ // Arrange
+ var renderer = new ImageRenderer(new ImageRendererOptions { ImageFormat = ImageFormat.Png });
+ IBarcode barcode = QrEncoder.Encode("Hello", ErrorCorrectionLevel.L, Encoding.Unicode);
+
+ // Act
+ var image = renderer.Render(barcode);
+
+ // Assert
+ image.Frames.Count.Should().Be(1);
+ image.Width.Should().Be(310);
+ image.Height.Should().Be(310);
+ image.PixelType.BitsPerPixel.Should().Be(8);
+ }
+
private static byte[] RenderBarcodeToByteArray(IRenderer renderer, IBarcode barcode)
{
using var stream = new MemoryStream();