首页 > 解决方案 > 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);
}

标签: c#.netimagewinformsprintdocument

解决方案


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);
}

推荐阅读