Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print high quality Tiff documents from picturebox C#

I'm using WinForms. My program opens .Tiff image documents into a picturebox. The problem I'm having is printing high quality tiff images from the picturebox. I've tried and tested printing many times. When the document prints the words are not crisp/clear its a little blurry. I tested my printer also to check if there was a problem with my printer. I printed a regular text document using Microsoft word and it printed out clearly, so its an issue with my code.

Can someone provide me with the code to print in high quality .tiff images in my picturebox? Would I need to increase my DPI programmatically to increase the quality of the image?

enter image description here

This is my code.

    private void DVPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

        e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        e.Graphics.DrawImage(pictureBox1.Image, 25, 25, 800, 1050);

    }


    private void Print_button_Click(object sender, EventArgs e)
    {
        PrintPreviewDialog.Document = PrintDocument;
        PrintPreviewDialog.ShowDialog();
    }

After analyzing my code further, I think I figured out why my pictures are turning out blurry. When I open a Tiff document, I have in my form a back, and forward button for the multiple tiff pages. In the RefreshImage method, I think that’s where my images are turning blurry. Here is my code:

    private int intCurrPage = 0; // defining the current page (its some sort of a counter)
    bool opened = false; // if an image was opened

    //-------------------------------------Next and Back Button-------------------------------------------------

    private void btn_Back_Click(object sender, EventArgs e)
    {
        if (opened) // the button works if the file is opened. you could go with button.enabled
        {
            if (intCurrPage == 0) // it stops here if you reached the bottom, the first page of the tiff
            { intCurrPage = 0; }
            else
            {
                intCurrPage--; // if its not the first page, then go to the previous page
                RefreshImage(); // refresh the image on the selected page
            }
        }
    }

    private void btn_Next_Click(object sender, EventArgs e)
    {
        if (opened) // the button works if the file is opened. you could go with button.enabled
        {
            if (intCurrPage == Convert.ToInt32(lblNumPages.Text)) // if you have reached the last page it ends here
                                                                  // the "-1" should be there for normalizing the number of pages
            { intCurrPage = Convert.ToInt32(lblNumPages.Text); }
            else
            {
                intCurrPage++;
                RefreshImage();
            }
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var bm = new Bitmap(pictureBox1.Image);
        bm.SetResolution(600, 600);
        Image image1 = new Bitmap(bm);
        pictureBox1.Image = image1;
        pictureBox1.Refresh();
    }

    public void RefreshImage()
    {
        Image myImg; // setting the selected tiff
        Image myBmp; // a new occurance of Image for viewing

        myImg = System.Drawing.Image.FromFile(@lblFile.Text); // setting the image from a file

        int intPages = myImg.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page); // getting the number of pages of this tiff
        intPages--; // the first page is 0 so we must correct the number of pages to -1
        lblNumPages.Text = Convert.ToString(intPages); // showing the number of pages
        lblCurrPage.Text = Convert.ToString(intCurrPage); // showing the number of page on which we're on

        myImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, intCurrPage); // going to the selected page

        myBmp = new Bitmap(myImg, pictureBox1.Width, pictureBox1.Height); // setting the new page as an image
        // Description on Bitmap(SOURCE, X,Y)

        pictureBox1.Image = myBmp; // showing the page in the pictureBox1

    }

enter image description here

like image 875
taji01 Avatar asked Sep 27 '22 13:09

taji01


1 Answers

    myBmp = new Bitmap(myImg, pictureBox1.Width, pictureBox1.Height); 
    pictureBox1.Image = myBmp;

Look no further, you are rescaling the image to fit the picture box. That throws away lots of pixels in the source image, the picture box is a lot smaller than the image. The aspect ratio is only correct by accident btw.

Simplest way is to just not rescale it yourself and leave it up to the PictureBox control to do it. Albeit that it makes painting slow. The other way is to simply keep a reference to the original image. Just use a variable instead of the PictureBox.Image property.

like image 67
2 revs Avatar answered Sep 30 '22 08:09

2 revs