/// <summary> /// Helper function to create an ArtQRCode graphic with a single function call /// </summary> /// <param name="plainText">Text/payload to be encoded inside the QR code</param> /// <param name="pixelsPerModule">Amount of px each dark/light module of the QR code shall take place in the final QR code image</param> /// <param name="darkColor">Color of the dark modules</param> /// <param name="lightColor">Color of the light modules</param> /// <param name="backgroundColor">Color of the background</param> /// <param name="eccLevel">The level of error correction data</param> /// <param name="forceUtf8">Shall the generator be forced to work in UTF-8 mode?</param> /// <param name="utf8BOM">Should the byte-order-mark be used?</param> /// <param name="eciMode">Which ECI mode shall be used?</param> /// <param name="requestedVersion">Set fixed QR code target version.</param> /// <param name="backgroundImage">A bitmap object that will be used as background picture</param> /// <param name="pixelSizeFactor">Value between 0.0 to 1.0 that defines how big the module dots are. The bigger the value, the less round the dots will be.</param> /// <param name="drawQuietZones">If true a white border is drawn around the whole QR Code</param> /// <param name="quietZoneRenderingStyle">Style of the quiet zones</param> /// <param name="backgroundImageStyle">Style of the background image (if set). Fill=spanning complete graphic; DataAreaOnly=Don't paint background into quietzone</param> /// <param name="finderPatternImage">Optional image that should be used instead of the default finder patterns</param> /// <returns>QRCode graphic as bitmap</returns> public static Bitmap GetQRCode(string plainText, int pixelsPerModule, Color darkColor, Color lightColor, Color backgroundColor, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, Bitmap backgroundImage = null, double pixelSizeFactor = 0.8, bool drawQuietZones = true, QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle.Flat, BackgroundImageStyle backgroundImageStyle = BackgroundImageStyle.DataAreaOnly, Bitmap finderPatternImage = null) { using (var qrGenerator = new QRCodeGenerator()) using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion)) using (var qrCode = new ArtQRCode(qrCodeData)) return(qrCode.GetGraphic(pixelsPerModule, darkColor, lightColor, backgroundColor, backgroundImage, pixelSizeFactor, drawQuietZones, quietZoneRenderingStyle, backgroundImageStyle, finderPatternImage)); }
/// <summary> /// Renders an art-style QR code with dots as modules and various user settings /// </summary> /// <param name="pixelsPerModule">Amount of px each dark/light module of the QR code shall take place in the final QR code image</param> /// <param name="darkColor">Color of the dark modules</param> /// <param name="lightColor">Color of the light modules</param> /// <param name="backgroundColor">Color of the background</param> /// <param name="backgroundImage">A bitmap object that will be used as background picture</param> /// <param name="pixelSizeFactor">Value between 0.0 to 1.0 that defines how big the module dots are. The bigger the value, the less round the dots will be.</param> /// <param name="drawQuietZones">If true a white border is drawn around the whole QR Code</param> /// <param name="quietZoneRenderingStyle">Style of the quiet zones</param> /// <param name="backgroundImageStyle">Style of the background image (if set). Fill=spanning complete graphic; DataAreaOnly=Don't paint background into quietzone</param> /// <param name="finderPatternImage">Optional image that should be used instead of the default finder patterns</param> /// <returns>QRCode graphic as bitmap</returns> public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Color backgroundColor, Bitmap backgroundImage = null, double pixelSizeFactor = 0.8, bool drawQuietZones = true, QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle.Dotted, BackgroundImageStyle backgroundImageStyle = BackgroundImageStyle.DataAreaOnly, Bitmap finderPatternImage = null) { if (pixelSizeFactor > 1) { throw new Exception("The parameter pixelSize must be between 0 and 1. (0-100%)"); } int pixelSize = (int)Math.Min(pixelsPerModule, Math.Floor(pixelsPerModule / pixelSizeFactor)); var numModules = QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8); var offset = (drawQuietZones ? 0 : 4); var size = numModules * pixelsPerModule; var bitmap = new Bitmap(size, size); using (var graphics = Graphics.FromImage(bitmap)) { using (var lightBrush = new SolidBrush(lightColor)) { using (var darkBrush = new SolidBrush(darkColor)) { // make background transparent using (var brush = new SolidBrush(backgroundColor)) graphics.FillRectangle(brush, new Rectangle(0, 0, size, size)); //Render background if set if (backgroundImage != null) { if (backgroundImageStyle == BackgroundImageStyle.Fill) { graphics.DrawImage(Resize(backgroundImage, size), 0, 0); } else if (backgroundImageStyle == BackgroundImageStyle.DataAreaOnly) { var bgOffset = 4 - offset; graphics.DrawImage(Resize(backgroundImage, size - (2 * bgOffset * pixelsPerModule)), 0 + (bgOffset * pixelsPerModule), (bgOffset * pixelsPerModule)); } } var darkModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, darkBrush); var lightModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, lightBrush); for (var x = 0; x < numModules; x += 1) { for (var y = 0; y < numModules; y += 1) { var rectangleF = new Rectangle(x * pixelsPerModule, y * pixelsPerModule, pixelsPerModule, pixelsPerModule); var pixelIsDark = this.QrCodeData.ModuleMatrix[offset + y][offset + x]; var solidBrush = pixelIsDark ? darkBrush : lightBrush; var pixelImage = pixelIsDark ? darkModulePixel : lightModulePixel; if (!IsPartOfFinderPattern(x, y, numModules, offset)) { if (drawQuietZones && quietZoneRenderingStyle == QuietZoneStyle.Flat && IsPartOfQuietZone(x, y, numModules)) { graphics.FillRectangle(solidBrush, rectangleF); } else { graphics.DrawImage(pixelImage, rectangleF); } } else if (finderPatternImage == null) { graphics.FillRectangle(solidBrush, rectangleF); } } } if (finderPatternImage != null) { var finderPatternSize = 7 * pixelsPerModule; graphics.DrawImage(finderPatternImage, new Rectangle(0, 0, finderPatternSize, finderPatternSize)); graphics.DrawImage(finderPatternImage, new Rectangle(size - finderPatternSize, 0, finderPatternSize, finderPatternSize)); graphics.DrawImage(finderPatternImage, new Rectangle(0, size - finderPatternSize, finderPatternSize, finderPatternSize)); } graphics.Save(); } } } return(bitmap); }