c# 使用打印机打印并设置打印位置及宽高
1.在界面中使用自带的控件printDocument
2.将以下函数绑定到控件的PrintPage事件
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { try { if (CurrtPrintImage != null)//此处是打印一个图像,CurrtPrintImage是Image类型 { float x; float y; float width; float height; switch (comboBox1.Text)//此处此页共可打印多少个图像 { case "8"://总个数为8 string location = comboBox2.Text;//此处是将当前图像打印到此页的哪个位置。 if (Convert.ToInt32(location) % 2 != 0) //奇数1,3,5,7 { x = printDocument1.DefaultPageSettings.PaperSize.Width / 4.0f - printDocument1.DefaultPageSettings.PaperSize.Height / 8 + 20; y = (printDocument1.DefaultPageSettings.PaperSize.Height / 4.0f) * (Convert.ToInt32(location) / 2); } else //偶数2,4,6,8 { x = printDocument1.DefaultPageSettings.PaperSize.Width / 4.0f * 3 - printDocument1.DefaultPageSettings.PaperSize.Height / 8 - 20; y = (printDocument1.DefaultPageSettings.PaperSize.Height / 4.0f) * (Convert.ToInt32(location) / 2 - 1); } width = printDocument1.DefaultPageSettings.PaperSize.Height / 4.0f; height = printDocument1.DefaultPageSettings.PaperSize.Height / 4.0f; e.Graphics.DrawImage(CurrtPrintImage, x, y, width, height); break; case "6": location = comboBox2.Text; if (Convert.ToInt32(location) % 2 != 0) //奇数1,3,5 { x = printDocument1.DefaultPageSettings.PaperSize.Width / 4f - printDocument1.DefaultPageSettings.PaperSize.Height / 8 - 40; y = (printDocument1.DefaultPageSettings.PaperSize.Height / 3f) * (Convert.ToInt32(location) / 2); } else //偶数2,4,6 { x = printDocument1.DefaultPageSettings.PaperSize.Width / 4f * 3 - printDocument1.DefaultPageSettings.PaperSize.Height / 8 - 55; y = (printDocument1.DefaultPageSettings.PaperSize.Height / 3f) * (Convert.ToInt32(location) / 2 - 1); } width = printDocument1.DefaultPageSettings.PaperSize.Height / 3f; height = printDocument1.DefaultPageSettings.PaperSize.Height / 3f; e.Graphics.DrawImage(CurrtPrintImage, x, y, width, height); break; case "4": location = comboBox2.Text; if (Convert.ToInt32(location) % 2 != 0) //奇数1,3 { x = printDocument1.DefaultPageSettings.PaperSize.Width / 4f - printDocument1.DefaultPageSettings.PaperSize.Height / 8 - 70; y = (printDocument1.DefaultPageSettings.PaperSize.Height / 2f) * (Convert.ToInt32(location) / 2) + 70 + ((location == "1" || location == "2") ? 30 : -30); } else //偶数2,4 { x = printDocument1.DefaultPageSettings.PaperSize.Width / 4f * 3 - printDocument1.DefaultPageSettings.PaperSize.Height / 8 - 70; y = (printDocument1.DefaultPageSettings.PaperSize.Height / 2f) * (Convert.ToInt32(location) / 2 - 1) + 70 + ((location == "1" || location == "2") ? 30 : -30); } width = printDocument1.DefaultPageSettings.PaperSize.Height / 2.7f; height = printDocument1.DefaultPageSettings.PaperSize.Height / 2.7f; e.Graphics.DrawImage(CurrtPrintImage, x, y, width, height); break; case "2": location = comboBox2.Text; if (Convert.ToInt32(location) % 2 != 0) //奇数1 { x = printDocument1.DefaultPageSettings.PaperSize.Width / 2f - printDocument1.DefaultPageSettings.PaperSize.Height / 4; y = (printDocument1.DefaultPageSettings.PaperSize.Height / 2f) * (Convert.ToInt32(location) - 1); } else //偶数2 { x = printDocument1.DefaultPageSettings.PaperSize.Width / 2f - printDocument1.DefaultPageSettings.PaperSize.Height / 4; y = (printDocument1.DefaultPageSettings.PaperSize.Height / 2f) * (Convert.ToInt32(location) - 1); } width = printDocument1.DefaultPageSettings.PaperSize.Height / 2f; height = printDocument1.DefaultPageSettings.PaperSize.Height / 2f; e.Graphics.DrawImage(CurrtPrintImage, x, y, width, height); break; } } } catch (Exception exception) { } }
private void Print(){
if (comboBox1.SelectedIndex < 0 || comboBox2.SelectedIndex < 0) { MessageBox.Show("请选择打印的版数以及位置"); return; } printDocument1.DocumentName = "示例图像"; printDocument1.PrinterSettings.PrinterName = "示例打印机名称"; foreach (System.Drawing.Printing.PaperSize item in printDocument1.PrinterSettings.PaperSizes) { if (item.PaperName=="A4") { printDocument1.DefaultPageSettings.PaperSize = item; printDocument1.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0); //设置边距 break; } } printDocument1.Print();//开始打印
}
浙公网安备 33010602011771号