Save QR code as image

pull/45/head
manuelbl 7 years ago
parent 0818d857c1
commit 13e17fbbe0

@ -26,6 +26,8 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
namespace Io.Nayuki.QrCodeGen namespace Io.Nayuki.QrCodeGen
{ {
@ -356,6 +358,54 @@ namespace Io.Nayuki.QrCodeGen
return 0 <= x && x < Size && 0 <= y && y < Size && _modules[y, x]; return 0 <= x && x < Size && 0 <= y && y < Size && _modules[y, x];
} }
/// <summary>
/// Returns a raster image depicting this QR Code, with the specified module scale and border modules.
/// </summary>
/// <remarks>
/// For example, <c>ToImage(scale: 10, border: 4)</c> means to pad the QR Code with 4 white
/// border modules on all four sides, and use 10&#xD7;10 pixels to represent each module.
/// The resulting image only contains the hex colors 000000 and FFFFFF.
/// </remarks>
/// <param name="scale">the side length (measured in pixels, must be positive) of each module</param>
/// <param name="border">the number of border modules to add, which must be non-negative</param>
/// <returns>a new image representing this QR Code, with padding and scaling</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the scale is 0 or negative, if the border is negative
/// or if the resulting image is wider than 32,768 pixels</exception>
public Bitmap ToImage(int scale, int border)
{
if (scale <= 0)
{
throw new ArgumentOutOfRangeException(nameof(scale), "Value out of range");
}
if (border < 0)
{
throw new ArgumentOutOfRangeException(nameof(border), "Value out of range");
}
int dim = (Size + border * 2) * scale;
if (dim > short.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large");
}
Bitmap bitmap = new Bitmap(dim, dim, PixelFormat.Format24bppRgb);
// simple and inefficient
for (var y = 0; y < dim; y++)
{
for (var x = 0; x < dim; x++)
{
bool color = GetModule(x / scale - border, y / scale - border);
bitmap.SetPixel(x, y, color ? Color.Black : Color.White);
}
}
return bitmap;
}
#endregion #endregion

@ -16,4 +16,8 @@
<PackageReleaseNotes>Initial release for .NET</PackageReleaseNotes> <PackageReleaseNotes>Initial release for .NET</PackageReleaseNotes>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
</ItemGroup>
</Project> </Project>

@ -0,0 +1,45 @@
/*
* QR Code generator library (.NET)
*
* Copyright (c) Project Nayuki. (MIT License)
* https://www.nayuki.io/page/qr-code-generator-library
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
using System.Drawing.Imaging;
using Xunit;
using static Io.Nayuki.QrCodeGen.QrCode;
namespace Io.Nayuki.QrCodeGen.Test
{
public class PngTest
{
[Fact]
private void PngImage()
{
var qrCode = EncodeText("The quick brown fox jumps over the lazy dog", Ecc.High);
using (var bitmap = qrCode.ToImage(3, 4))
{
Assert.Equal(135, bitmap.Width);
Assert.Equal(135, bitmap.Height);
bitmap.Save("qrcode.png", ImageFormat.Png);
}
}
}
}
Loading…
Cancel
Save