C#打印过程中高清的问题处理

设置图片DPI,先放大图片再打印,有一定的效果


        public void SetImageDpi(string imagePath, float dpiX, float dpiY)
        {
            // 加载图像
            using (System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath))
            {
                // 将图像转换为Bitmap,以便设置DPI
                using (Bitmap bitmap = new Bitmap(image))
                {
                    // 设置X和Y方向的DPI
                    bitmap.SetResolution(dpiX, dpiY);

                    // 保存修改后的图像,可以选择不同的格式和质量
                    bitmap.Save("output_image.png", System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
 

更新图片大小
    static public void ResizeImage(string originalImagePath, string resizedImagePath)
    {
       
        using (System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath))
        {
            int width = originalImage.Width * 2, height = originalImage.Height * 2;

            using (Bitmap resizedImage = new Bitmap(width, height))
            {
                using (Graphics graphics = Graphics.FromImage(resizedImage))
                {
                    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    graphics.DrawImage(originalImage, 0, 0, width, height);

                    // Save the resized image to file.
                    resizedImage.Save(resizedImagePath, ImageFormat.Bmp);
                }
            }
        }
    }
 

一张图片一张图片的打印

    public void Print(string imagePath)
    {
        // 创建一个PrintDocument对象
        PrintDocument printDocument = new PrintDocument();

        // 设置PrintPage事件处理程序
        printDocument.PrintPage += (sender, e) =>
        {
            // 加载要打印的图像
            System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath);//可以直接把图像获取在按钮里面做,我们Print操作形参设为图像Image也是可以的

            // 计算图像在打印页面上的位置和大小
            RectangleF imageBounds = e.MarginBounds;
            float imageAspectRatio = image.Width / (float)image.Height;
            if (imageAspectRatio > 1)
            {
                imageBounds.Width = e.MarginBounds.Width;
                imageBounds.Height = e.MarginBounds.Width / imageAspectRatio;
            }
            else
            {
                imageBounds.Height = e.MarginBounds.Height;
                imageBounds.Width = e.MarginBounds.Height * imageAspectRatio;
            }
            /*
             我们使用e.MarginBounds获取打印页面的边距,并将其赋值给imageBounds变量。然后,我们计算图像的宽高比,通过将图像的宽度除以高度来获得。
             如果图像的宽高比大于1,表示图像比较宽,我们将imageBounds的宽度设置为边距的宽度,然后根据宽高比计算出相应的高度。如果图像的宽高比小于等于1,
             表示图像比较高,我们将imageBounds的高度设置为边距的高度,然后根据宽高比计算出相应的宽度。
             */
            // 绘制图像在打印页面上
            e.Graphics.DrawImage(image, imageBounds);

            // 通知打印系统还有更多页面需要打印
            e.HasMorePages = false;

            // 释放图像资源
            image.Dispose();
        };
    }
 

参考文章:

https://www.cnblogs.com/kingkie/p/17288150.html  C#高清打印关键代码

  • 19
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值