打印清晰的TIF图像

时间:2016-05-31 21:06:27

标签: c# .net image winforms printing

我正在使用WinForms,在我的表单中我有一个图片框和一个打印按钮。问题是(TIF)图像在打印后不断变形。在print_preview_Settings()我定义自己的边距。为了测试,我将tif图像打印成PDF,从那里你可以看到明显的失真。如何使此图像打印清晰?

以下是我使用的测试.tif图像的链接。

http://www.filedropper.com/display_1

这是我目前正在使用的代码:

    using System.Drawing.Printing;

    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox1.Load(@"C:\image\display.tif");
    }

    private void Print_Maintating_Aspect_Ratio_Of_Image(PrintPageEventArgs e)
    {
        var img_width = e.PageBounds.Width - e.MarginBounds.Left - Math.Abs(e.MarginBounds.Right - e.PageBounds.Width);
        var img_height = e.PageBounds.Height - e.MarginBounds.Top - Math.Abs(e.MarginBounds.Bottom - e.PageBounds.Height);


        var img = ResizeAcordingToImage(pictureBox1.Image, img_width, img_height);

        e.Graphics.DrawImage(img,
            e.MarginBounds.Left, e.MarginBounds.Top, img.Width, img.Height);
    }

    private Image ResizeAcordingToImage(Image Source, int boxWidth, int boxHeight)
    {
        Image resizedImage;
        double dbl = (double)Source.Width / (double)Source.Height;
        //set height of image to boxHeight and check if resulting width is less than boxWidth, 
        //else set width of image to boxWidth and calculate new height
        if ((int)((double)boxHeight * dbl) <= boxWidth)
        {
            resizedImage = new Bitmap(Source, (int)((double)boxHeight * dbl), boxHeight);
        }
        else
        {
            resizedImage = new Bitmap(Source, boxWidth, (int)((double)boxWidth / dbl));
        }

        return resizedImage;

    }

    private void Print_btn_Click(object sender, EventArgs e)
    {
        if (printDialog1.ShowDialog() == DialogResult.OK)
        {
            printDocument1.Print();
        }
    }

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        print_preview_Settings();

        Print_Maintating_Aspect_Ratio_Of_Image(e);
    }

    private void print_preview_Settings()
    {
        try {
            printPreviewDialog1.Document = printDocument1;

            int Top = 2;
            int Left = 1;
            int Right = 1;
            int Bottom = 3;

            printDocument1.DefaultPageSettings.Margins.Top = Top;
            printDocument1.DefaultPageSettings.Margins.Left = Left;
            printDocument1.DefaultPageSettings.Margins.Right = Right;
            printDocument1.DefaultPageSettings.Margins.Bottom = Bottom;


            printPreviewDialog1.ShowDialog();
        }
        catch(Exception ex)
        {
            MessageBox.Show("Documented printed, but you got an error: "+ ex.Message);
        }
    }

enter image description here

0 个答案:

没有答案