c# - 提高图像的打印质量

标签 c# printing

我正在编写一个需要打印高分辨率 .tiff 图像的程序。我遇到的问题是我无法打印高质量的 .tiff。由于 .tiff 的尺寸较大(例如 8700x7200),它不适合任何标准尺寸的纸张。我尝试增加 DPI,但这似乎没有任何效果。我能让 .tiff 适合页面的唯一方法是缩小它。但随后图像具有可怕的质量。 (我将其缩小以适合 11x17,但它的指示分辨率仅为 1100x1700)。我尝试更改打印机上的分辨率设置,尝试手动和以编程方式设置打印机质量/分辨率,但没有成功。基本上我希望能够将更多像素的 .tiff 放到 11x17 页面上,这样我就不必缩放太多。我认为增加打印 dpi 会增加 11x17 英寸的像素数量,但我看不出有什么效果。也许我做错了什么。任何帮助将不胜感激。谢谢。

下面的代码是我在调用 pd.Print() 时尝试立即执行的操作。

    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        //float x = ev.MarginBounds.Left;
        //float y = ev.MarginBounds.Top;
        try
        {
            //ResizeImage(@"H:\21RR-G0K-30140-0220-0002.tiff", @"H:\21RR-G0K-30140-0220-0002-new.tiff", 500, 900, false);
            Image tempImage = Image.FromFile(@"H:\21RR-G0K-30140-0220-0002.tiff");
            Bitmap bMap = new Bitmap(tempImage);
            bMap.SetResolution(1200, 1200);
            string l = "";
            tempImage = bMap;
            /*if (tempImage.Width > tempImage.Height)  //if poster is longer then it is tall, rotate the image. Done to match standard printing aspect ratios
            {
                Bitmap tempBitmap = new Bitmap(tempImage); //Need to convert to Bitmap type to do rotation
                RotateBicubic rotationFilter = new RotateBicubic(90, true);
                tempImage = rotationFilter.Apply(tempBitmap);
            }*/
            float ImageAspectRatio = (float)tempImage.Height / (float)tempImage.Width;  
            float PageSizeAspectRatio = (float)_pSize.Height / (float)_pSize.Width; //_pSize is the selected printing sheet size
            if (ImageAspectRatio < 1 && PageSizeAspectRatio > 1) //Need to rotate Image. Can't attempt to rotate tempImage due to size. Scale then rotate.
            {
                double x_scale = (double)_pSize.Width / (double)tempImage.Height;
                double y_scale = (double)_pSize.Height / (double)tempImage.Width;
                int percent = 0;
                if (y_scale < x_scale)
                {
                    percent = Convert.ToInt32(y_scale * 100);
                }
                else
                {
                    percent = Convert.ToInt32(x_scale * 100);
                }
                Image myImage = ImageManipulation.ScaleByPercent(tempImage, percent); //I know this line is the problem, but I can't fit the image on the page without massive scaling due to the page reolution restraints
                Bitmap tempMap = new Bitmap(myImage);
                tempMap.SetResolution(1200, 1200);
                RotateBicubic rotateBC = new RotateBicubic(90);
                Image finalImage = rotateBC.Apply(tempMap); //rotate the image 90 degrees using bicubic interpolation. This isn't what's killing the quality as the quality is no better with this disabled
                ev.Graphics.DrawImage(finalImage, 0, 0);
            }
            else if (ImageAspectRatio >= 1 && PageSizeAspectRatio >= 1) //No Need for rotation 
            {
                double x_scale = (double)_pSize.Width / (double)tempImage.Width;
                double y_scale = (double)_pSize.Height / (double)tempImage.Height;
                int percent = 0;
                if (y_scale < x_scale)
                {
                    percent = Convert.ToInt32(y_scale * 100);
                }
                else
                {
                    percent = Convert.ToInt32(x_scale * 100);
                }

                Image myImage = ImageManipulation.ScaleByPercent(tempImage, percent);
                ev.Graphics.DrawImage(myImage, 0, 0);

            }

            else
            {

            }              
        }
        catch(Exception ex)
        {
            string breakingpoint = "";
        }
    }

最佳答案

您要查找的术语是“重采样”。

使用设备坐标系或 GDI+ HIENGLISH 单位缩放图像可能也是最好的(我相信这是在打印设备上下文中使用的。)

使用 DrawImage 命令,您将从 GDI+ 获得的最佳效果是 HighQualityBicubic Resampling。


    Private Sub pd_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage

        Dim img As Image = Image.FromFile("SAMPLE.TIF")

        Dim imageWidth As Integer = CInt((img.Width / img.HorizontalResolution) * 100)
        Dim imageHeight As Integer = CInt((img.Height / img.VerticalResolution) * 100)

        If imageWidth > e.PageBounds.Width Then
            Dim ratio As Double = imageWidth / imageHeight
            imageWidth = e.PageBounds.Width
            If ratio < 1 Then
                imageHeight = CInt(imageWidth * ratio)
            Else
                imageHeight = CInt(imageWidth / ratio)
            End If
        End If

        If imageHeight > e.PageBounds.Height Then
            Dim ratio As Double = imageHeight / imageWidth
            imageHeight = e.PageBounds.Height
            If ratio < 1 Then
                imageWidth = CInt(imageHeight * ratio)
            Else
                imageWidth = CInt(imageHeight / ratio)
            End If
        End If

        e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        e.Graphics.DrawImage(img, 0, 0, imageWidth, imageHeight)

    End Sub

关于c# - 提高图像的打印质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1079222/

相关文章:

c# - 将存储库引入 AccountController

javascript - JS 函数没有正确返回值

c# - 如何注册通用服务

c# - 命令执行期间遇到 MySQL fatal error - 循环遍历文件夹中的所有 csv 文件以加载数据本地 infile

C# Entity Framework 排序和分页给出意想不到的结果

netbeans - 在 NetBeans 中打印项目

c++ - 我在哪里可以获得适用于 Qt 5.1.0 或 Qt 5.1.1 的兼容 NCreport 库

c# - FlowLayoutPanel 中奇怪的空格

c++ - 为什么矩阵没有正确初始化/打印?

printing - 使用 Ghostscript 将 PCL 转换为 PostScript