|
| 1 | +// |
| 2 | +// QR code generator library (.NET) |
| 3 | +// https://github.com/manuelbl/QrCodeGenerator |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Manuel Bleichenbacher |
| 6 | +// Licensed under MIT License |
| 7 | +// https://opensource.org/licenses/MIT |
| 8 | +// |
| 9 | + |
| 10 | +using SkiaSharp; |
| 11 | +using System; |
| 12 | +using System.IO; |
| 13 | + |
| 14 | +namespace Net.Codecrete.QrCodeGenerator |
| 15 | +{ |
| 16 | + public static class QrCodeBitmapExtensions |
| 17 | + { |
| 18 | + /// <inheritdoc cref="ToBitmap(QrCode, int, int)"/> |
| 19 | + /// <param name="background">The background color.</param> |
| 20 | + /// <param name="foreground">The foreground color.</param> |
| 21 | + public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border, SKColor foreground, SKColor background) |
| 22 | + { |
| 23 | + // check arguments |
| 24 | + if (scale <= 0) |
| 25 | + { |
| 26 | + throw new ArgumentOutOfRangeException(nameof(scale), "Value out of range"); |
| 27 | + } |
| 28 | + if (border < 0) |
| 29 | + { |
| 30 | + throw new ArgumentOutOfRangeException(nameof(border), "Value out of range"); |
| 31 | + } |
| 32 | + |
| 33 | + int size = qrCode.Size; |
| 34 | + int dim = (size + border * 2) * scale; |
| 35 | + |
| 36 | + if (dim > short.MaxValue) |
| 37 | + { |
| 38 | + throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large"); |
| 39 | + } |
| 40 | + |
| 41 | + // create bitmap |
| 42 | + SKBitmap bitmap = new SKBitmap(dim, dim, SKColorType.Rgb888x, SKAlphaType.Opaque); |
| 43 | + |
| 44 | + using (SKCanvas canvas = new SKCanvas(bitmap)) |
| 45 | + { |
| 46 | + // draw background |
| 47 | + using (SKPaint paint = new SKPaint { Color = background }) |
| 48 | + { |
| 49 | + canvas.DrawRect(0, 0, dim, dim, paint); |
| 50 | + } |
| 51 | + |
| 52 | + // draw modules |
| 53 | + using (SKPaint paint = new SKPaint { Color = foreground }) |
| 54 | + { |
| 55 | + for (int y = 0; y < size; y++) |
| 56 | + { |
| 57 | + for (int x = 0; x < size; x++) |
| 58 | + { |
| 59 | + if (qrCode.GetModule(x, y)) |
| 60 | + { |
| 61 | + canvas.DrawRect((x + border) * scale, (y + border) * scale, scale, scale, paint); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return bitmap; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Creates a bitmap (raster image) of this QR code. |
| 73 | + /// <para> |
| 74 | + /// The <paramref name="scale"/> parameter specifies the scale of the image, which is |
| 75 | + /// equivalent to the width and height of each QR code module. Additionally, the number |
| 76 | + /// of modules to add as a border to all four sides can be specified. |
| 77 | + /// </para> |
| 78 | + /// <para> |
| 79 | + /// For example, <c>ToBitmap(scale: 10, border: 4)</c> means to pad the QR code with 4 white |
| 80 | + /// border modules on all four sides, and use 10×10 pixels to represent each module. |
| 81 | + /// </para> |
| 82 | + /// <para> |
| 83 | + /// The resulting bitmap uses the pixel format <see cref="PixelFormat.Format24bppRgb"/>. |
| 84 | + /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). |
| 85 | + /// </para> |
| 86 | + /// </summary> |
| 87 | + /// <param name="scale">The width and height, in pixels, of each module.</param> |
| 88 | + /// <param name="border">The number of border modules to add to each of the four sides.</param> |
| 89 | + /// <returns>The created bitmap representing this QR code.</returns> |
| 90 | + /// <exception cref="ArgumentOutOfRangeException"><paramref name="scale"/> is 0 or negative, <paramref name="border"/> is negative |
| 91 | + /// or the resulting image is wider than 32,768 pixels.</exception> |
| 92 | + public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border) |
| 93 | + { |
| 94 | + return qrCode.ToBitmap(scale, border, SKColors.Black, SKColors.White); |
| 95 | + } |
| 96 | + |
| 97 | + /// <inheritdoc cref="ToPng(QrCode, int, int)"/> |
| 98 | + /// <param name="background">The background color.</param> |
| 99 | + /// <param name="foreground">The foreground color.</param> |
| 100 | + public static byte[] ToPng(this QrCode qrCode, int scale, int border, SKColor foreground, SKColor background) |
| 101 | + { |
| 102 | + using SKBitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); |
| 103 | + using SKData data = bitmap.Encode(SKEncodedImageFormat.Png, 90); |
| 104 | + return data.ToArray(); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Creates a PNG image of this QR code and returns it as a byte array. |
| 109 | + /// <para> |
| 110 | + /// The <paramref name="scale"/> parameter specifies the scale of the image, which is |
| 111 | + /// equivalent to the width and height of each QR code module. Additionally, the number |
| 112 | + /// of modules to add as a border to all four sides can be specified. |
| 113 | + /// </para> |
| 114 | + /// <para> |
| 115 | + /// For example, <c>ToPng(scale: 10, border: 4)</c> means to pad the QR code with 4 white |
| 116 | + /// border modules on all four sides, and use 10×10 pixels to represent each module. |
| 117 | + /// </para> |
| 118 | + /// <para> |
| 119 | + /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). |
| 120 | + /// </para> |
| 121 | + /// </summary> |
| 122 | + /// <param name="scale">The width and height, in pixels, of each module.</param> |
| 123 | + /// <param name="border">The number of border modules to add to each of the four sides.</param> |
| 124 | + /// <returns>The created bitmap representing this QR code.</returns> |
| 125 | + /// <exception cref="ArgumentOutOfRangeException"><paramref name="scale"/> is 0 or negative, <paramref name="border"/> is negative |
| 126 | + /// or the resulting image is wider than 32,768 pixels.</exception> |
| 127 | + public static byte[] ToPng(this QrCode qrCode, int scale, int border) |
| 128 | + { |
| 129 | + return qrCode.ToPng(scale, border, SKColors.Black, SKColors.White); |
| 130 | + } |
| 131 | + |
| 132 | + /// <inheritdoc cref="SaveAsPng(QrCode, string, int, int)"/> |
| 133 | + /// <param name="background">The background color.</param> |
| 134 | + /// <param name="foreground">The foreground color.</param> |
| 135 | + public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border, SKColor foreground, SKColor background) |
| 136 | + { |
| 137 | + using SKBitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); |
| 138 | + using SKData data = bitmap.Encode(SKEncodedImageFormat.Png, 90); |
| 139 | + using FileStream stream = File.OpenWrite(filename); |
| 140 | + data.SaveTo(stream); |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Saves this QR code as a PNG file. |
| 145 | + /// <para> |
| 146 | + /// The <paramref name="scale"/> parameter specifies the scale of the image, which is |
| 147 | + /// equivalent to the width and height of each QR code module. Additionally, the number |
| 148 | + /// of modules to add as a border to all four sides can be specified. |
| 149 | + /// </para> |
| 150 | + /// <para> |
| 151 | + /// For example, <c>SaveAsPng("qrcode.png", scale: 10, border: 4)</c> means to pad the QR code with 4 white |
| 152 | + /// border modules on all four sides, and use 10×10 pixels to represent each module. |
| 153 | + /// </para> |
| 154 | + /// <para> |
| 155 | + /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). |
| 156 | + /// </para> |
| 157 | + /// </summary> |
| 158 | + /// <param name="scale">The width and height, in pixels, of each module.</param> |
| 159 | + /// <param name="border">The number of border modules to add to each of the four sides.</param> |
| 160 | + /// <exception cref="ArgumentOutOfRangeException"><paramref name="scale"/> is 0 or negative, <paramref name="border"/> is negative |
| 161 | + /// or the resulting image is wider than 32,768 pixels.</exception> |
| 162 | + public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border) |
| 163 | + { |
| 164 | + qrCode.SaveAsPng(filename, scale, border, SKColors.Black, SKColors.White); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments