Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created February 18, 2014 13:20
Show Gist options
  • Save podhmo/9070829 to your computer and use it in GitHub Desktop.
Save podhmo/9070829 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Markup;
namespace QR.qrcode
{
public class QRCodeCanvas : Canvas
{
public readonly Brush InverseForeground;
public static readonly DependencyProperty QRCodeProperty =
DependencyProperty.Register("QRCode", typeof(string), typeof(QRCodeCanvas));
public string QRCode
{
get { return (string)GetValue(QRCodeProperty); }
set { SetValue(QRCodeProperty, value); }
}
public static readonly DependencyProperty ForegroundProperty =
DependencyProperty.Register("Foreground", typeof(Brush), typeof(QRCodeCanvas));
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
public QRCodeCanvas()
: base()
{
this.Loaded += QRCodeCanvas_Loaded;
this.InverseForeground = new SolidColorBrush(Color.FromRgb(255,255,255));
}
void QRCodeCanvas_Loaded(object sender, RoutedEventArgs e)
{
if(this.QRCode == null)
return;
// temporary
var w = 3;
var h = 3;
var y = 0;
var x = 0;
var path = new Path() { Fill = this.Foreground, Stroke = this.Foreground };
var group = new GeometryGroup();
var writer = new ZXing.QrCode.QRCodeWriter();
var matrix = writer.encode(this.QRCode, ZXing.BarcodeFormat.QR_CODE, 96, 96);
for (var i = 0; i < matrix.Height; i++)
{
for (var j = 0; j < matrix.Width; j++)
{
if (matrix[j, i] == true)
{
var rect = new RectangleGeometry() { Rect = new Rect(x, y, w, h) };
group.Children.Add(rect);
}
x += w;
}
y += h;
x = 0;
}
path.Data = group;
this.Children.Add(path);
this.Loaded -= QRCodeCanvas_Loaded;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment