1.有弹出窗口打印
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument printDocument1 = new PrintDocument();
//printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GX430t";//打印机名称设置
//printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", label_biaoda1.Width, label_biaoda1.Height);//设置纸张大小
printDocument1.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = printDocument1;
DialogResult result = printPreviewDialog1.ShowDialog();
if (result == DialogResult.OK)
printDocument1.Print();
}
//以下代码获取到一张用作打印内容的图片
private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Image image = Image.FromFile(@"D:\0000-临时文件\4.png");
Bitmap _NewBitmap = (Bitmap)img;
/* e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);//调整图片的大小
e.Graphics.DrawImage(_NewBitmap, 0, 0, 570, 380);*/
//各种调整,消除锯齿
/*e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
_NewBitmap.SetResolution(583, 390);*/
e.Graphics.DrawImage(image, 0, 0, image.Width, image.Height);
//_NewBitmap.Save("D:\\0000-临时文件\\1.png", System.Drawing.Imaging.ImageFormat.Png);//图片保存
}
2.无弹出窗口打印
内含绘图打印内容,二维码生成,条形码生成。
private void button2_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printDocument_PrintA4Page);
pd.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GX430t"; //打印机名称
pd.DefaultPageSettings.Landscape = true; //设置横向打印,不设置默认是纵向的
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
//pd.PrinterSettings.Copies = (short)copies; //打印份数
pd.Print();
}
//设置打印的内容
private void PrintDocument_PrintA4Page(object sender, PrintPageEventArgs e)
{
LabelInfo ProductLabelInfo = RowChangeLabelInfo();
//用于打印
Brush solidBrush = new SolidBrush(Color.Black);//画刷
Pen p = new Pen(System.Drawing.Color.Black); //线条颜色
Font fntTxt1 = new Font("微软雅黑", 8, FontStyle.Bold);//动态内容字体
Font fntTxt2 = new Font("微软雅黑", 9, FontStyle.Bold);//动态内容字体
try
{
//居中
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(ProductLabelInfo.ProductName, fntTxt2, solidBrush, new Rectangle(110, 90, 116, 30), sf);//point,size 26
e.Graphics.DrawString(ProductLabelInfo.Quality, fntTxt2, solidBrush, new Rectangle(310, 90, 76, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.SteelGrade, fntTxt1, solidBrush, new Rectangle(110, 120, 116, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.Date, fntTxt1, solidBrush, new Rectangle(310, 120, 76, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.CoilNo, fntTxt1, solidBrush, new Rectangle(110, 150, 116, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.SurfaceQuality, fntTxt1, solidBrush, new Rectangle(310, 150, 76, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.Size, fntTxt1, solidBrush, new Rectangle(110, 180, 116, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.NW, fntTxt1, solidBrush, new Rectangle(310, 180, 76, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.Standard, fntTxt1, solidBrush, new Rectangle(110, 210, 116, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.GW, fntTxt1, solidBrush, new Rectangle(310, 210, 76, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.SurfaceStructure, fntTxt1, solidBrush, new Rectangle(480, 210, 70, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.ContractNo, fntTxt1, solidBrush, new Rectangle(110, 240, 116, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.WeightMarker, fntTxt2, solidBrush, new Rectangle(310, 240, 76, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.CoatingMass, fntTxt1, solidBrush, new Rectangle(480, 240, 70, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.SurfTreatment, fntTxt2, solidBrush, new Rectangle(110, 270, 116, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.PackagedForm, fntTxt2, solidBrush, new Rectangle(310, 270, 76, 30), sf);
e.Graphics.DrawString(ProductLabelInfo.ReferenceLength, fntTxt1, solidBrush, new Rectangle(480, 270, 70, 30), sf);//492
Bitmap bitmap = LabelInfo.CreateQRCode(ProductLabelInfo.ToStringPrint()); //绘制二维码
bitmap.MakeTransparent(Color.White);//跟随颜色调整
e.Graphics.DrawImage(bitmap, 350, 30, 240, 240);//二维码所处位置
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//生成二维码
public static Bitmap CreateQRCode(string asset)//字符串内容为二维码内容
{
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8", //编码
Width = 80, //宽度
Height = 80 //高度
};
BarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = options
};
return writer.Write(asset);
}
//左对齐函数(可用于调整二维码内字符的位置)
public static string strLeft(string str)
{
Encoding coding = Encoding.Default;
int count = 0;
foreach (char ch in str.ToCharArray())
{
int byteCount = coding.GetByteCount(ch.ToString());
if (byteCount == 2)
count++;
}
string result = str.PadRight(18 - count);
return result;
}
//生成条形码
public static void CreateBarCode(string filePath, string barCodeContent)
{
ZXing.Common.EncodingOptions options = new ZXing.Common.EncodingOptions()
{
Height = 120,//设置宽高
Width = 200,
};
//生成条形码的图片并保存
ZXing.BarcodeWriter wr = new ZXing.BarcodeWriter()
{
Options = options,//进行指定规格
Format = ZXing.BarcodeFormat.CODE_128,//条形码的规格
};
System.Drawing.Bitmap img = wr.Write(barCodeContent);//生成图片
img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
System.Diagnostics.Process.Start(filePath);
}
个人记录,大家可参考。