C# 我将条形码图像生成到图片框,然后写入打印。是否有任何方法可以将生成的图像直接写入打印文档?
我已生成条形码图像并加载到图片框,然后将其写入打印文档。是否有任何方法可以将生成的图像直接写入打印文档 我的代码是:C# 我将条形码图像生成到图片框,然后写入打印。是否有任何方法可以将生成的图像直接写入打印文档?,c#,.net,image,winforms,printdocument,C#,.net,Image,Winforms,Printdocument,我已生成条形码图像并加载到图片框,然后将其写入打印文档。是否有任何方法可以将生成的图像直接写入打印文档 我的代码是: // Code to generate barcode image private void btnGen_Click(object sender, EventArgs e) { printDoc.DefaultPageSettings.PaperSize = new PaperSize("3x2 Inc", 300, 200); BarcodeWrite
// 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
返回位图
您可以直接使用它
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);
}
将位图字段添加到表单类(例如,
private Bitmap currentBarCode=null;
),将其设置为currentBarCode=writer.Write(txtCode.Text)
,显示:pbBar.Image=currentBarCode编码>,打印:e.Graphics.DrawImage(当前条形码,20,40)
(如果需要,可以使用重载DrawImage()
来指定目标矩形)但是要小心使用Bitmap
类;如果您没有在using
块中使用它们,则在使用完这些对象后,需要显式地释放它们。