Skip to content

Optimization #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions QRCoder/Base64QRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 35 additions & 5 deletions QRCoder/QRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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
{
Expand Down Expand Up @@ -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
Expand Down