Changing Image Resolution?

Administrator

VB.NET Forum Admin
Joined
Jun 3, 2004
Messages
1,462
Programming Experience
10+
Hello,

I know there is a SetResolution method on the Image class, however, what I want to do is like in PhotoShop - Change the resolution from 300 px/inch to 72 px/inch and the image resizes. When I set the resolution to 72,72 the resolution changes but the image itself is not resizing as I do in PhotoShop. Anyone have any experience with this?
 
Calling SetResolution on a Bitmap will not change the image size (dimension), to do that you will have to create a new sized Bitmap and draw in the original scaled (,this is also called resampling and changes the image by spreading same pixel data over a larger or smaller dimension.) The resolution does not affect onscreen display, usually the graphics adapter is configured for 96*96 dpi and all images are displayed for this resolution only by their dimensions. For printing resolution is handled differently, where the ink "dot" size is given by printer resolution and physical measurements like "inch" is absolute. So if you save both your images with same dimension size at different resolutions and display them in any image viewer they look the same, then try print preview (the Paint application has one accurate) and you will see that they will look very different on paper. It is the same that happens when you display these image in Pictureboxes or draw them to a PrintDocument and preview this in .Net application. If Photoshop changes the image display for different resolutions automatically it must be reflecting printing display and not screen display. Another example is Paint.Net that has a resize dialog where you can also change resolution, the screen display remains but the dialog reveals how large the image will be for printing with separate values for pixel size and print size. It is discussed more in detail in this article: Understanding Bitmap Image Resolution
To display an image in printing resolution in a form you can create a big "paper canvas" Bitmap with default screen resolution and use Graphics.DrawImageUnscaled method to draw images with different resolutions, this will make them display with their relative printing size.
 
JohnH,

Here is some code I came up with, give it a try.

VB.NET:
    Friend Shared Function SetResolution(ByVal sourceImage As Image, ByVal resolution As Integer) As Image
        Dim reduction As Double = resolution / CInt(sourceImage.HorizontalResolution)
        Using newImage As New Bitmap(sourceImage.Width, sourceImage.Height, sourceImage.PixelFormat)
            newImage.SetResolution(resolution, resolution)
            Dim outImage As New Bitmap(sourceImage, CInt(sourceImage.Width * reduction), CInt(sourceImage.Height * reduction))
            Using g As Graphics = Graphics.FromImage(newImage)
                g.InterpolationMode = InterpolationMode.HighQualityBicubic
                g.DrawImage(outImage, 0, 0)
            End Using
            Return outImage
        End Using
    End Function
 
Back
Top