c#winform用zxing库生成二维码
最近闲来无事,写了一个二维码生成器,拿来和移动端对接。
使用的东西最主要还是sqlite数据库和DevExpress控件和zxing库
界面生成二维码有两种方式: 一种简单二维码,一种带logo的二维码
部分生成二维码代码
1)简单二维码
·ByteMatrix byteMatrix = new MultiFormatWriter().encode(_message, BarcodeFormat.QR_CODE, 220, 260);
int width = byteMatrix.Width;
int height = byteMatrix.Height;
bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bmap.SetPixel(x, y, byteMatrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
}
}
this.pictureBox.Image = bmap;
2)带logo二维码
············Hashtable hashTable = new Hashtable();
hashTable.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//设置条码两边空白
hashTable.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//像素容错率
ByteMatrix byteMatrix = new MultiFormatWriter().encode(_message, BarcodeFormat.QR_CODE, 220, 220, hashTable);
int width = byteMatrix.Width;
int height = byteMatrix.Height;
Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//生成logo图片
Bitmap logo = new Bitmap(strLogoFilePath);
//画出二维码图片
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bmap.SetPixel(x, y, byteMatrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
}
}
//将图片插入在生成的二维码中
int middleW = width / 2 - 20;//图片所在位置
int middleH = height / 2 - 20;
//logo大小
int middleL = 40;
int middleT = 40;
bmpimg = new Bitmap(bmap.Width, bmap.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmpimg))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(bmap, 0, 0, width, height);
g.FillRectangle(Brushes.White, middleW, middleH, middleL, middleT);
g.DrawImage(logo, middleW, middleH, middleL, middleT);
}
this.pictureBox.Image = bmpimg;
不懂的地方可以留言询问哦