C# - How to Print a PictureBox [QRCode] in WinForm Application
生成二维码
- 通过 Nuget 安装 QRCoder
- 添加 PictureBox 控件时,将其 SizeMode 设置为 Zoom
二维码生成代码:
private void BtnGenerate_Click(object sender, EventArgs e)
{
var qg = new QRCoder.QRCodeGenerator();
var myData = qg.CreateQrCode(TxtCode.Text, QRCoder.QRCodeGenerator.ECCLevel.H);
var code = new QRCoder.QRCode(myData);
PicQRCode.Image = code.GetGraphic(50);
}
打印图像
利用 .NET Framework 自带的 PrintDocument 调用打印机。
private void PrintBarCode()
{
var pd = new PrintDialog();
var doc = new PrintDocument();
doc.PrintPage += Doc_PrintPage;
pd.Document = doc;
if (pd.ShowDialog() == DialogResult.OK)
{
doc.Print();
}
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
var bm = new Bitmap(PicQRCode.Width, PicQRCode.Height);
PicQRCode.DrawToBitmap(bm, new Rectangle(0, 0, PicQRCode.Width, PicQRCode.Height));
e.Graphics.DrawImage(bm, 0, 0);
bm.Dispose();
}