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.

74 lines
3.1 KiB

#if NET40 || NET461
using System;
using System.Windows;
using System.Windows.Media;
namespace Apewer.Internals.QrCode
{
internal class XamlQRCode : AbstractQRCode, IDisposable
{
/// <summary>
/// Constructor without params to be used in COM Objects connections
/// </summary>
public XamlQRCode() { }
public XamlQRCode(QRCodeData data) : base(data) { }
public DrawingImage GetGraphic(int pixelsPerModule)
{
return this.GetGraphic(pixelsPerModule, true);
}
public DrawingImage GetGraphic(int pixelsPerModule, bool drawQuietZones)
{
var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
var viewBox = new Size(pixelsPerModule * drawableModulesCount, pixelsPerModule * drawableModulesCount);
return this.GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);
}
public DrawingImage GetGraphic(Size viewBox, bool drawQuietZones = true)
{
return this.GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);
}
public DrawingImage GetGraphic(int pixelsPerModule, string darkColorHex, string lightColorHex, bool drawQuietZones = true)
{
var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
var viewBox = new Size(pixelsPerModule * drawableModulesCount, pixelsPerModule * drawableModulesCount);
return this.GetGraphic(viewBox, new SolidColorBrush((Color)ColorConverter.ConvertFromString(darkColorHex)), new SolidColorBrush((Color)ColorConverter.ConvertFromString(lightColorHex)), drawQuietZones);
}
public DrawingImage GetGraphic(Size viewBox, Brush darkBrush, Brush lightBrush, bool drawQuietZones = true)
{
var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
var qrSize = Math.Min(viewBox.Width, viewBox.Height);
var unitsPerModule = qrSize / drawableModulesCount;
var offsetModules = drawQuietZones ? 0 : 4;
DrawingGroup drawing = new DrawingGroup();
drawing.Children.Add(new GeometryDrawing(lightBrush, null, new RectangleGeometry(new Rect(new Point(0, 0), new Size(qrSize, qrSize)))));
var group = new GeometryGroup();
double x = 0d, y = 0d;
for (int xi = offsetModules; xi < drawableModulesCount; xi++)
{
y = 0d;
for (int yi = offsetModules; yi < drawableModulesCount; yi++)
{
if (this.QrCodeData.ModuleMatrix[yi][xi])
{
group.Children.Add(new RectangleGeometry(new Rect(x, y, unitsPerModule, unitsPerModule)));
}
y += unitsPerModule;
}
x += unitsPerModule;
}
drawing.Children.Add(new GeometryDrawing(darkBrush, null, group));
return new DrawingImage(drawing);
}
}
}
#endif