C#按物理尺寸打印图片 e.Graphics.DrawImage(image,0,0); //按物理尺寸打印标签,600点改为300点的打印机

46 篇文章 1 订阅

C#按物理尺寸打印图片

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;
            }
        }
    }
}

如果更改图片由300改为600点

注意:设置分辨率public void SetResolution(float xDpi, float yDpi);  水平和垂直分辨率,以每英寸点数为单位。打印机一般为600点。

如果由300改为600点,像素不变,那么物理尺寸会变小一倍,比如20变为10cm。还用300点打印机打印,会变小一倍。

如果标签打印机由600点改为300点的打印机

1.物理尺寸没变,像素没变,图没变。

2.分辨率变了,打印机由600点换为300点的。打印机是输出设备相当于显示器。

那是按物理尺寸打印,还是按分辨率打印?

按物理尺寸打印必然要压缩(上面源码结果)。按分辨率打印会变大一倍。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C#中,可以通过一些技术来提高图形绘制的速度,例如使用双缓冲、图像压缩、使用硬件加速等。其中,Graphics.DrawImage方法是用于绘制图像的常用方法之一。 以下是一些提高Graphics.DrawImage方法性能的技巧: 1. 双缓冲:在绘制过程中使用双缓冲技术可以减少闪烁。可以使用一个额外的缓冲区来绘制图像,然后将整个缓冲区一次性绘制到屏幕上。 ```csharp // 创建一个内存缓冲区 Bitmap buffer = new Bitmap(this.Width, this.Height); Graphics bufferGraphics = Graphics.FromImage(buffer); // 在缓冲区上进行绘制 bufferGraphics.DrawImage(image, x, y); // 将缓冲区绘制到屏幕上 e.Graphics.DrawImage(buffer, 0, 0); ``` 2. 图像压缩:如果需要在绘制时进行图像缩放,可以考虑使用适当的图像压缩算法来减少图像数据量,从而提高绘制速度。可以使用`Image.GetThumbnailImage`方法进行图像压缩。 ```csharp // 压缩图像 Image thumbnail = image.GetThumbnailImage(width, height, null, IntPtr.Zero); // 绘制压缩后的图像 e.Graphics.DrawImage(thumbnail, x, y); ``` 3. 硬件加速:使用硬件加速可以利用图形处理单元(GPU)来加速图形绘制。在C#中,可以使用一些第三方库如Direct2D或OpenGL来实现硬件加速的图形绘制。 这些技巧可以帮助提高Graphics.DrawImage方法的执行效率和绘制性能。但是请注意,具体的优化策略还需要根据实际场景和需求进行选择和调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值