获取当前设置能使用的打印机:
1 foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称 2 { 3 prints.Add(sPrint); 4 }
获取打印机下的纸盒:
1 PrinterSettings ps = new PrinterSettings(); 2 ps.PrinterName = printName; 3 for (int i = 0; i < ps.PaperSources.Count; i++) 4 { 5 sources.Add(ps.PaperSources[i].SourceName); 6 }
打印图片方法:
1 private static void PicturePrintDocument_PrintPage(object sender, PrintPageEventArgs e) 2 { 3 FileStream fs = File.OpenRead(filePath); 4 int filelength = 0; 5 filelength = (int)fs.Length; //获得文件长度 6 Byte[] image = new Byte[filelength]; //建立一个字节数组 7 fs.Read(image, 0, filelength); //按字节流读取 8 Image result = Image.FromStream(fs); 9 fs.Close(); 10 e.Graphics.DrawImage(result, 0, 0); //img大小 11 //e.Graphics.DrawString(TicCode, DrawFont, brush, 600, 600); //绘制字符串 12 e.HasMorePages = false; 13 }
注册打印方法:
1 using (PrintDocument pd = new PrintDocument()) { 2 pd.PrintPage += PicturePrintDocument_PrintPage; //注册打印事件 3 pd.PrinterSettings.PrinterName = printName; //打印机选择 4 pd.Print(); 5 pd.Dispose(); 6 }