/// <inheritdoc cref="Draw(QrCode, Graphics, float, float)"/> /// <param name="background">The background color.</param> /// <param name="foreground">The foreground color.</param> public static void Draw(this QrCode qrCode, Graphics graphics, float scale, float border, Color foreground, Color background) { if (scale <= 0 || border < 0) { return; } int size = qrCode.Size; float dim = (size + border * 2) * scale; // draw background if (background != null) { using SolidBrush brush = new SolidBrush(background); graphics.FillRectangle(brush, 0, 0, dim, dim); } // draw modules using (SolidBrush brush = new SolidBrush(foreground)) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (qrCode.GetModule(x, y)) { graphics.FillRectangle(brush, (x + border) * scale, (y + border) * scale, scale, scale); } } } } }
/// <inheritdoc cref="ToBitmap(QrCode, int, int)"/> /// <param name="background">The background color.</param> /// <param name="foreground">The foreground color.</param> public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border, SKColor foreground, SKColor background) { // check arguments 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 size = qrCode.Size; int dim = (size + border * 2) * scale; if (dim > short.MaxValue) { throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large"); } // create bitmap SKBitmap bitmap = new SKBitmap(dim, dim, SKColorType.Rgb888x, SKAlphaType.Opaque); using (SKCanvas canvas = new SKCanvas(bitmap)) { // draw background using (SKPaint paint = new SKPaint { Color = background }) { canvas.DrawRect(0, 0, dim, dim, paint); } // draw modules using (SKPaint paint = new SKPaint { Color = foreground }) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (qrCode.GetModule(x, y)) { canvas.DrawRect((x + border) * scale, (y + border) * scale, scale, scale, paint); } } } } } return(bitmap); }
/// <inheritdoc cref="ToBitmap(QrCode, int, int)"/> /// <param name="background">The background color.</param> /// <param name="foreground">The foreground color.</param> public static Image ToBitmap(this QrCode qrCode, int scale, int border, Color foreground, Color background) { // check arguments 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 size = qrCode.Size; int dim = (size + border * 2) * scale; if (dim > short.MaxValue) { throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large"); } // create bitmap Image <Rgb24> image = new Image <Rgb24>(dim, dim); image.Mutate(img => { // draw background img.Fill(background); // draw modules for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (qrCode.GetModule(x, y)) { img.Fill(foreground, new Rectangle((x + border) * scale, (y + border) * scale, scale, scale)); } } } }); return(image); }