c# - I generated barcode image to picture box and then wrote to printing. Is there any method to write generated image directly to print document?
问题描述
I have generated barcode image and loaded to picture box and then I wrote it to print document. Is there any method to write generated image directly to print document?
My code is :
// Code to generate barcode image
private void btnGen_Click(object sender, EventArgs e)
{
printDoc.DefaultPageSettings.PaperSize = new PaperSize("3x2 Inc", 300, 200);
BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.CODE_128 };
writer.Options = new ZXing.Common.EncodingOptions { Width = 250, Height = 100, Margin = 0 };
pbBar.Width = 250; // pbBar is my picture box.
pbBar.Height = 100;
pbBar.Image = writer.Write(txtCode.Text);
}
// Print document code
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics gr = e.Graphics;
Bitmap map = new Bitmap(pbBar.Width, pbBar.Height);
pbBar.DrawToBitmap(map, new Rectangle(0, 0, pbBar.Width, pbBar.Height));
gr.DrawImage(map, 20, 40);
}
解决方案
writer.Write
返回BitMap
您可以直接使用它
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.CODE_128 };
writer.Options = new ZXing.Common.EncodingOptions { Width = 250, Height = 100, Margin = 0 };
Graphics gr = e.Graphics;
gr.DrawImage(writer.Write(txtCode.Text), 20, 40);
}
推荐阅读
- python - 如何从列表列表中创建字典键?
- javascript - 不和谐每日问候
- javascript - 是否有 Chrome API 可以访问响应模式以调整到自定义宽度和高度?
- html - 如何仅使用 html 在某个网站中显示没有标题的另一个网站?
- linux - Traefik:所有子目录返回 404
- python-3.x - 我想找到所有包含某些单词/单词的标题行,这些单词/单词要被抓取并保存到文本文件中
- python - 使用变量将数字转换为科学计数法python
- laravel - 将验证错误从 Laravel 控制器发送回 Vue 组件的正确方法是什么?
- ios - iOS应用程序空白navigationView在某些设备模拟器上打开屏幕,而不是在其他设备模拟器上
- java - Java欧拉数结果无穷大