ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#按物理尺寸打印图片 e.Graphics.DrawImage(image,0,0); //按物理尺寸打印

2021-09-18 11:34:00  阅读:453  来源: 互联网

标签:image 打印 System Height Width 尺寸 new Drawing 物理


 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SYS_TEST.BaseClass
{

    /// <summary>
    /// 图片直接打印方法
    /// </summary>
    public class PrintDirectClass
    {
        public const int PRINT_INIT = 0;
        public const int PRINT_FINISH= 1;
        public const int PRINT_CANCE = 2;
        public const int PRINT_ERROR = 3;
        public const int PRINTING = 4;
        public int result = PRINT_INIT; //打印结果

        private int printNum = 0;//多页打印
        public Image image = null;
        public string imageFile = "";//单个图片文件
        //private ArrayList fileList = new ArrayList();//多个图片文件
        public PrintDirectClass(Image image)
        {
            this.image = image;
            PrintPreview();
        }
        public PrintDirectClass(string imageFile)
        {
            this.imageFile = imageFile;
            PrintPreview();
        }
        public void PrintPreview()
        {
            PrintDocument docToPrint = new PrintDocument();
            docToPrint.BeginPrint += new System.Drawing.Printing.PrintEventHandler(this.docToPrint_BeginPrint);
            docToPrint.EndPrint += new System.Drawing.Printing.PrintEventHandler(this.docToPrint_EndPrint);
            docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.docToPrint_PrintPage);
            docToPrint.DefaultPageSettings.Landscape = false;
  

            PrintDialog printDialog = new PrintDialog();
            printDialog.AllowSomePages = true;
            printDialog.ShowHelp = true;
            printDialog.Document = docToPrint;
            //if (printDialog.ShowDialog() == DialogResult.OK)
            //{
                docToPrint.Print();
            //}
            //else
            //{
            //    result=PRINT_CANCE;
            //}

        }
        private void docToPrint_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            result=PRINTING;
            //if (fileList.Count == 0)
            //    fileList.Add(imageFile);
        }
        private void docToPrint_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            result=PRINT_FINISH;
        }
        private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {         
                //图片抗锯齿
                //e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                if (this.image == null)
                {
                    //Stream fs = new FileStream(fileList[i].Trim(), FileMode.Open, FileAccess.Read);
                    Stream fs = new FileStream(imageFile.Trim(), FileMode.Open, FileAccess.Read);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
                }
          
                //int x = e.MarginBounds.X;
                //int y = e.MarginBounds.Y;
                e.Graphics.DrawImage(image,0,0); //按物理尺寸打印
                //int width = image.Width;
                //int height = image.Height;
                if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
                {
                    width = e.MarginBounds.Width;
                    height = image.Height * e.MarginBounds.Width / image.Width;
                }
                else
                {
                    height = e.MarginBounds.Height;
                    width = image.Width * e.MarginBounds.Height / image.Height;
                }

                DrawImage参数根据打印机和图片大小自行调整
                //System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
                //e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Point);
                //System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
                //System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(0, 0, width, height);
                //if (image.Height < 310)
                //{
                //    e.Graphics.DrawImage(image, 0, 30, image.Width + 20, image.Height);
                //    //    System.Drawing.Rectangle destRect1 = new System.Drawing.Rectangle(0, 30, image.Width, image.Height);
                //    //    e.Graphics.DrawImage(image, destRect1, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
                //}
                //else
                //{
                //    e.Graphics.DrawImage(image, 0, 0, image.Width + 20, image.Height);
                //    //    System.Drawing.Rectangle destRect2 = new System.Drawing.Rectangle(0, 0, image.Width, image.Height);
                //    //    e.Graphics.DrawImage(image, destRect2, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
                //}

                //if (printNum < fileList.Count - 1)
                //{
                //    printNum++;
                //    e.HasMorePages = true;//HasMorePages为true则再次运行PrintPage事件
                //    return;
                //}
                e.HasMorePages = false;
            }
            catch (Exception ex) {
                result = PRINT_ERROR;
            }
        }
    }
}

 

标签:image,打印,System,Height,Width,尺寸,new,Drawing,物理
来源: https://blog.csdn.net/chenhao0568/article/details/120363997

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有