diff --git a/dotnet/QrCodeGenerator/QrCode.cs b/dotnet/QrCodeGenerator/QrCode.cs index 7137203..7f6d4d0 100644 --- a/dotnet/QrCodeGenerator/QrCode.cs +++ b/dotnet/QrCodeGenerator/QrCode.cs @@ -26,6 +26,8 @@ using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Drawing; +using System.Drawing.Imaging; namespace Io.Nayuki.QrCodeGen { @@ -356,6 +358,54 @@ namespace Io.Nayuki.QrCodeGen return 0 <= x && x < Size && 0 <= y && y < Size && _modules[y, x]; } + + /// + /// Returns a raster image depicting this QR Code, with the specified module scale and border modules. + /// + /// + /// For example, ToImage(scale: 10, border: 4) means to pad the QR Code with 4 white + /// border modules on all four sides, and use 10×10 pixels to represent each module. + /// The resulting image only contains the hex colors 000000 and FFFFFF. + /// + /// the side length (measured in pixels, must be positive) of each module + /// the number of border modules to add, which must be non-negative + /// a new image representing this QR Code, with padding and scaling + /// Thrown if the scale is 0 or negative, if the border is negative + /// or if the resulting image is wider than 32,768 pixels + 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 diff --git a/dotnet/QrCodeGenerator/QrCodeGenerator.csproj b/dotnet/QrCodeGenerator/QrCodeGenerator.csproj index 37a22d2..ad02b4c 100644 --- a/dotnet/QrCodeGenerator/QrCodeGenerator.csproj +++ b/dotnet/QrCodeGenerator/QrCodeGenerator.csproj @@ -16,4 +16,8 @@ Initial release for .NET + + + + diff --git a/dotnet/QrCodeGeneratorTest/PngTest.cs b/dotnet/QrCodeGeneratorTest/PngTest.cs new file mode 100644 index 0000000..d65041a --- /dev/null +++ b/dotnet/QrCodeGeneratorTest/PngTest.cs @@ -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); + } + } + } +}