diff --git a/QRCoder/Base64QRCode.cs b/QRCoder/Base64QRCode.cs index cf55e21d..7f9a60cb 100644 --- a/QRCoder/Base64QRCode.cs +++ b/QRCoder/Base64QRCode.cs @@ -57,6 +57,24 @@ public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, } return base64; } + public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true, ImageType imgType = ImageType.Png, bool drawRoundedCorners=true) + { + var base64 = string.Empty; + using (Bitmap bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, icon, iconSizePercent, iconBorderWidth, drawQuietZones,drawRoundedCorners)) + { + base64 = BitmapToBase64(bmp, imgType); + } + return base64; + } + public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true, ImageType imgType = ImageType.Png, bool drawRoundedCorners = true, bool renderPartlyHiddenModule = true) + { + var base64 = string.Empty; + using (Bitmap bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, icon, iconSizePercent, iconBorderWidth, drawQuietZones, drawRoundedCorners, renderPartlyHiddenModule)) + { + base64 = BitmapToBase64(bmp, imgType); + } + return base64; + } private string BitmapToBase64(Bitmap bmp, ImageType imgType) diff --git a/QRCoder/QRCode.cs b/QRCoder/QRCode.cs index 1de595e9..3c8f7d20 100644 --- a/QRCoder/QRCode.cs +++ b/QRCoder/QRCode.cs @@ -58,7 +58,7 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, return bmp; } - public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon=null, int iconSizePercent=15, int iconBorderWidth = 6, bool drawQuietZones = true) + public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon=null, int iconSizePercent=15, int iconBorderWidth = 6, bool drawQuietZones = true, bool drawRoundedCorners = true, bool renderPartlyHiddenModule = true) { var size = (this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8)) * pixelsPerModule; var offset = drawQuietZones ? 0 : 4 * pixelsPerModule; @@ -86,7 +86,15 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, iconY = (bmp.Height - iconDestHeight) / 2; var centerDest = new RectangleF(iconX - iconBorderWidth, iconY - iconBorderWidth, iconDestWidth + iconBorderWidth * 2, iconDestHeight + iconBorderWidth * 2); - iconPath = this.CreateRoundedRectanglePath(centerDest, iconBorderWidth * 2); + if (drawRoundedCorners) + { + iconPath = this.CreateRoundedRectanglePath(centerDest, iconBorderWidth * 2); + } + else + { + iconPath = this.CreateRectanglePath(centerDest); + } + } for (var x = 0; x < size + offset; x = x + pixelsPerModule) @@ -101,9 +109,20 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, if (drawIconFlag) { - var region = new Region(r); - region.Exclude(iconPath); - gfx.FillRegion(darkBrush, region); + if (renderPartlyHiddenModule) + { + var region = new Region(r); + region.Exclude(iconPath); + gfx.FillRegion(darkBrush, region); + } + else + { + if (!iconPath.GetBounds().IntersectsWith(r)) + { + gfx.FillRectangle(darkBrush,r); + } + } + } else { @@ -143,6 +162,17 @@ internal GraphicsPath CreateRoundedRectanglePath(RectangleF rect, int cornerRadi roundedRect.CloseFigure(); return roundedRect; } + + internal GraphicsPath CreateRectanglePath(RectangleF rect) + { + var angularRect = new GraphicsPath(); + angularRect.AddLine(rect.X, rect.Y, rect.Right, rect.Y); + angularRect.AddLine(rect.Right, rect.Y, rect.Right, rect.Y + rect.Height); + angularRect.AddLine(rect.Right, rect.Bottom, rect.X, rect.Bottom); + angularRect.AddLine(rect.X, rect.Bottom, rect.X, rect.Y); + angularRect.CloseFigure(); + return angularRect; + } } public static class QRCodeHelper