微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我将条形码图像生成到图片框,然后写入打印有什么方法可以将生成的图像直接写到打印文档中?

如何解决我将条形码图像生成到图片框,然后写入打印有什么方法可以将生成的图像直接写到打印文档中?

我已生成条形码图像并将其加载到图片框,然后将其写入以打印文档。有什么方法可以将生成的图像直接写到打印文档中?

我的代码是:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。