Change image resolution in C#


This example shows how to change an image’s resolution. It’s another example I created to help while writing books. Computers usually work with images that have a resolution of 96 pixels per inch (PPI). Printers and publishers often require images with higher resolution such as 300 dots per inch (DPI). This example shows how to convert an image from one resolution to another. While I was at it, I also decided to make the program change the image’s size if you want to do so.

When you invoke the File menu’s Open command, the following code executes.

private void mnuFileOpen_Click(object sender, EventArgs e)
    if (ofdOriginal.ShowDialog() == DialogResult.OK)
        OriginalBitmap = new Bitmap(ofdOriginal.FileName);
        pictureBox1.Image = OriginalBitmap;
        using (Graphics gr = Graphics.FromImage(OriginalBitmap))
            txtDpiX.Text = gr.DpiX.ToString();
            txtDpiY.Text = gr.DpiY.ToString();
        txtWidth.Text = OriginalBitmap.Width.ToString();
        txtHeight.Text = OriginalBitmap.Height.ToString();
        mnuFileSaveAs.Enabled = true;

This code displays an Open File Dialog to let you select an image file. If you pick a file and click Open, the program loads the file into a bitmap. It creates a Graphics object associated with the bitmap and uses its DpiX and DpiY properties to display the original image’s resolution. It also displays the bitmap’s Width and Height.

To change the image’s resolution or dimensions, simply type into the textboxes. Then when you invoke the File menu’s Save As command, the following code executes.

private void mnuFileSaveAs_Click(object sender, EventArgs e)
    if (sfdNew.ShowDialog() == DialogResult.OK)
        int old_wid = OriginalBitmap.Width;
        int old_hgt = OriginalBitmap.Height;
        int new_wid = int.Parse(txtWidth.Text);
        int new_hgt = int.Parse(txtHeight.Text);

        using (Bitmap bm = new Bitmap(new_wid, new_hgt))
            Point[] points =
                new Point(0, 0),
                new Point(new_wid, 0),
                new Point(0, new_hgt),
            using (Graphics gr = Graphics.FromImage(bm))
                gr.DrawImage(OriginalBitmap, points);
            float dpix = float.Parse(txtDpiX.Text);
            float dpiy = float.Parse(txtDpiY.Text);
            bm.SetResolution(dpix, dpiy);
            SaveImage(bm, sfdNew.FileName);

This code displays a Save File Dialog. If you select a file and click Save, the code reads the new values in the textboxes. It then creates a bitmap that has the new dimensions. It creates an associated Graphics object and uses its SetResolution method to set the new bitmap’s resolution. Finally the method saves the new bitmap.

For information on the SaveImage method that saves the file, see the post Save images with an appropriate format depending on the file name’s extension in C#.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in image processing and tagged , , , , , , , , , , , , , . Bookmark the permalink.

5 Responses to Change image resolution in C#

  1. Thank you so much for posting this. I just wasted a day trying to get SSRS 2012 to pump out a 300 dpi file.

    I finally just xformed the file using your method.

  2. Thank you so much for the code. Why can we not just change the height and width values and pass them to SetResolution() method?
    I tried it and it did not work; that is why I am asking:-)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.