Click here to Skip to main content
Email Password   helpLost your password?

Introduction  

This is the most common part to add an image / signature in any web/Windows applications. This article will show you how to resize an image keeping with the best graphics quality.

Background

Most of the time, developers face various problems when they resize an image runtime. In this article, I try to resize an image runtime with keeping the best graphics quality.

Using the Code 

Here I write a function to resize an image (i.e., “ImageResize”), this function has four input parameters that are:

  1. strImageSrcPath: A string that contains the image source file path
  2. strImageDesPath: A string that contains the destination file path
  3. intWidth: An integer value for the new image width
  4. intHeight : An integer value for the new image Height

After successfully calling the function with the valid input values, it will resize and store the image into your given destination path and return the full path to show the new resized image. Here I use .NET framework 3.5 (System.Drawing,IO) namespaces. 

 Public Function ImageResize(ByVal strImageSrcPath As String _
                          , ByVal strImageDesPath As String _
                          , Optional ByVal intWidth As Integer = 0 _
                          , Optional ByVal intHeight As Integer = 0) As String

    If System.IO.File.Exists(strImageSrcPath) = False Then Exit Function

    Dim objImage As System.Drawing.Image = System.Drawing.Image.FromFile(strImageSrcPath)

    If intWidth > objImage.Width Then intWidth = objImage.Width
    If intHeight > objImage.Height Then intHeight = objImage.Height
    If intWidth = 0 And intHeight = 0 Then
        intWidth = objImage.Width
        intHeight = objImage.Height
    ElseIf intHeight = 0 And intWidth <> 0 Then
        intHeight = Fix(objImage.Height * intWidth / objImage.Width)
    ElseIf intWidth = 0 And intHeight <> 0 Then
        intWidth = Fix(objImage.Width * intHeight / objImage.Height)
    End If

    Dim imgOutput As New Bitmap(objImage, intWidth, intHeight)
    Dim imgFormat = objImage.RawFormat

    objImage.Dispose()
    objImage = Nothing

    If strImageSrcPath = strImageDesPath Then System.IO.File.Delete(strImageSrcPath)
    ' send the resized image to the viewer
    imgOutput.Save(strImageDesPath, imgFormat)
    imgOutput.Dispose()

    Return strImageDesPath

End Function

Now we are going to discuss how the above function works. Here, you will find various methods to perform file manipulation and all these are common. I would like to highlight the main properties, methods and class that are used for image resizing.

Property

Methods

Bitmap Class

Encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes. A Bitmap is an object used to work with images defined by pixel data.

The concept is very simple, at first we need to create an instance of  System.Drawing.Image.FromFile and set the new width and height of the image. After that, we use Bitmap object to  populate the new image and set the actual format by using RawFormat property and finally store the new image into the destination path.

Conclusion

This is a very simple and easy way. I hope that it might be helpful to you. Enjoy!

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralThanks
vickypamgmail
23:13 28 Jul '09  
Thanks , it helped me.
GeneralRe: Thanks
Md. Marufuzzaman
3:11 29 Jul '09  
Thanks for reading my article... I m so happy that it's relay help you..Smile

Don't forget to click "Good Answer" on the post(s) that helped you.

Thanks
Md. Marufuzzaman

GeneralCan it resize a gif image with multi frames?
taihip
22:59 9 Jun '09  
Can it resize a gif image with multi frames?
GeneralA generic error occurred in GDI+
Member 3447522
20:39 9 Jun '09  
I received this error...
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+

...with the following code (on the line with imgOutput.Save). Any suggestions?

ImageResize(strSrcWithPath, strDest, 400, 400)

Public Function ImageResize(ByVal strSrcWithPath As String, ByVal strDest As String, _
Optional ByVal intHgt As Integer = 0, Optional ByVal intWid As Integer = 0) As String
''If File.Exists(strSrcWithPath) = False Then Exit Function
Dim objImage As Drawing.Image = Drawing.Image.FromFile(strSrcWithPath)
Dim intNewHgt As Integer = objImage.Height, intNewWid As Integer = objImage.Width, sglFactor As Single
If intNewHgt > 400 Or intNewWid > 400 Then
If intNewHgt > intNewWid Then
sglFactor = intHgt / intNewHgt
Else
sglFactor = intWid / intNewWid
End If
End If
intHgt = Fix(intNewHgt * sglFactor)
intWid = Fix(intNewWid * sglFactor)
Dim imgOutput As New Drawing.Bitmap(objImage, intWid, intHgt)
Dim imgFormat = objImage.RawFormat
objImage.Dispose()
objImage = Nothing

If strSrcWithPath = strDest Then Delete(strSrcWithPath)
'send the resized image to the viewer
imgOutput.Save(strDest, imgFormat)
imgOutput.Dispose()

Return strDest
End Function
GeneralRe: A generic error occurred in GDI+
Member 3447522
22:21 9 Jun '09  
Got it to work. In place of "strDest" on the imgOutput.Save line, I put in "strSrcWithPath" and it worked just fine.
GeneralRe: A generic error occurred in GDI+
aa_zz
22:21 9 Jul '09  
Hi You.
Can you support resize image bitmap 24bits ? I am need hurry.
not using MFC, GUI.
using VC++6.0 and win32 console

input (1.file bitmap. 2(scale or (width, leng))
output (file bitmap new)

I wish your help. thanks very much.
regards

nothing

GeneralMy vote of 1
Nagy Vilmos
3:01 9 Jun '09  
There is no real substance to this article. All you do is make use of the BitMap constructor to provide a re-sized image.
GeneralRe: My vote of 1
Member 3447522
21:44 9 Jun '09  
Um, I think that's why the article is titled "An easy way to resize an image." It presents stuff that I didn't know.


Last Updated 10 Jun 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010