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();//开始打印
}
复制代码

 

posted @   迷海  阅读(2032)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示