110,538
社区成员
发帖
与我相关
我的任务
分享
private System.Drawing.Printing.PrintDocument printDocument1;
private void Form1_Load(object sender, EventArgs e)
{
axBarCodeCtrl1.Value = "BM1159999";
printDocument1 = new System.Drawing.Printing.PrintDocument();
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
}
Bitmap memorybitmap;
/// <summary>
/// 导出条码为bmp
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
Graphics g = axBarCodeCtrl1.CreateGraphics();
memorybitmap = new Bitmap(axBarCodeCtrl1.Size.Width, axBarCodeCtrl1.Size.Height, g);
g = Graphics.FromImage(memorybitmap);
g.CopyFromScreen(this.Location.X + axBarCodeCtrl1.Location.X, this.Location.Y + axBarCodeCtrl1.Location.Y + SystemInformation.CaptionHeight, 0, 0, this.Size);
memorybitmap.Save(@"d:\Barcode\b.bmp");//保存为图片
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(printBmp, new Point(0, 0));
}
Bitmap printBmp;
/// <summary>
/// 打印条码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
try
{
Graphics g = axBarCodeCtrl1.CreateGraphics();
printBmp = new Bitmap(axBarCodeCtrl1.Size.Width, axBarCodeCtrl1.Size.Height, g);
g = Graphics.FromImage(printBmp);
g.CopyFromScreen(this.Location.X + axBarCodeCtrl1.Location.X, this.Location.Y + axBarCodeCtrl1.Location.Y + SystemInformation.CaptionHeight, 0, 0, this.Size);
printDocument1.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}