程子 orange of backroom 2月22日
一码在手,天下我有
如今是二维码遍地的时代,扫一扫方便了大家的生活,今天我们就用C#在winfrom窗体上制作维码
那么维码是什么呢,其实就是机器看的懂的但又不想让人看到的一个技术,比如你拿手机对着一本书的文字扫描,是什么都扫不到的(但是当今技术的发展已经可以了,但并不能说替代维码),废话不多说了
制作维码有很多种方法
在工程中引用ZXing.Net
在项目中,点击项目名称右键-->管理NuGet程序包,打开NuGet包管理器窗口,进行搜索下载即可,可在搜索框输入ZXing
private void button1_Click(object sender, EventArgs e)
{
EncodingOptions encodeOption = new EncodingOptions();
encodeOption.Height = 50; // 高度、宽度
encodeOption.Width = 120;
ZXing.BarcodeWriter wr = new BarcodeWriter();
wr.Options = encodeOption;
wr.Format = BarcodeFormat.CODE_39; // 条形码规格
Bitmap img = wr.Write("D1234B678A"); // 生成图片
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\EAN_13-" + "test" + ".jpg";
img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
private void button2_Click(object sender, EventArgs e)
{
ZXing.QrCode.QrCodeEncodingOptions qrEncodeOption = new ZXing.QrCode.QrCodeEncodingOptions();
qrEncodeOption.CharacterSet = "UTF-8"; // 设置编码格式,否则读取'中文'乱码
qrEncodeOption.Height = 30;
qrEncodeOption.Width = 30;
qrEncodeOption.Margin = 1; // 设置周围空白边距
// 2.生成条形码图片并保存
ZXing.BarcodeWriter wr = new BarcodeWriter();
wr.Format = BarcodeFormat.DATA_MATRIX; // 二维码
wr.Options = qrEncodeOption;
Bitmap img = wr.Write("D1234B678A");
string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\QR-" + "test2" + ".jpg";
img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
pictureBox1.Load(filePath);
}
private void btnPrint_Click(object sender, EventArgs e)
{
this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 220, 120);
//PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
//printPreviewDialog.Document = printDocument1; 打印预览代码
try
{
printDocument1.Print();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap img = new Bitmap(Image.FromFile(System.AppDomain.CurrentDomain.BaseDirectory + "\\EAN_13-" + "test" + ".jpg"));
e.Graphics.DrawString("A1234B678A", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 5, 5);
e.Graphics.DrawImage(img, 10, 30);
//e.Graphics.DrawString("D1234B678A", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 10, 90);
e.Graphics.DrawImage(pictureBox1.Image, 150, 80);
}
简单代码就是这样,原文:
https://blog.csdn.net/mycoolme5/article/details/85323407
https://www.jb51.net/article/136698.htm