You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.2 KiB

using System;
using System.Collections.Generic;
using System.Drawing;
#if NET40 || NET461
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
namespace Apewer.Internals.QrCode
{
internal class Base64QRCode : AbstractQRCode, IDisposable
{
private QRCode qr;
/// <summary>
/// Constructor without params to be used in COM Objects connections
/// </summary>
public Base64QRCode() {
qr = new QRCode();
}
public Base64QRCode(QRCodeData data) : base(data) {
qr = new QRCode(data);
}
public override void SetQRCodeData(QRCodeData data) {
this.qr.SetQRCodeData(data);
}
public string GetGraphic(int pixelsPerModule)
{
return this.GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
}
public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
{
return this.GetGraphic(pixelsPerModule, ColorTranslator.FromHtml(darkColorHtmlHex), ColorTranslator.FromHtml(lightColorHtmlHex), drawQuietZones, imgType);
}
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
{
var base64 = string.Empty;
using (Bitmap bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, drawQuietZones))
{
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)
{
var base64 = string.Empty;
using (Bitmap bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, icon, iconSizePercent, iconBorderWidth, drawQuietZones))
{
base64 = BitmapToBase64(bmp, imgType);
}
return base64;
}
private string BitmapToBase64(Bitmap bmp, ImageType imgType)
{
var base64 = string.Empty;
ImageFormat iFormat;
switch (imgType) {
case ImageType.Png:
iFormat = ImageFormat.Png;
break;
case ImageType.Jpeg:
iFormat = ImageFormat.Jpeg;
break;
case ImageType.Gif:
iFormat = ImageFormat.Gif;
break;
default:
iFormat = ImageFormat.Png;
break;
}
using (MemoryStream memoryStream = new MemoryStream())
{
bmp.Save(memoryStream, iFormat);
base64 = Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
}
return base64;
}
public enum ImageType
{
Gif,
Jpeg,
Png
}
}
}
#endif