Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我将条形码图像生成到图片框,然后写入打印。是否有任何方法可以将生成的图像直接写入打印文档?_C#_.net_Image_Winforms_Printdocument - Fatal编程技术网

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
块中使用它们,则在使用完这些对象后,需要显式地释放它们。