Click here to Skip to main content
6,594,932 members and growing! (15,910 online)
Email Password   helpLost your password?
Languages » VB.NET » General License: The Code Project Open License (CPOL)

An Easy Way to Resize an Image

By Md. Marufuzzaman

This article will show you how to resize an image keeping with the best graphics quality.
VB, .NET (.NET 2.0, .NET 3.0, .NET 3.5), Visual Studio (VS2008)
Version:12 (See All)
Posted:8 Jun 2009
Updated:9 Jun 2009
Views:5,764
Bookmarked:22 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
18 votes for this article.
Popularity: 4.93 Rating: 3.93 out of 5
1 vote, 5.6%
1

2
4 votes, 22.2%
3
2 votes, 11.1%
4
11 votes, 61.1%
5

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

  • RawFormat: Gets the file format of this image

Methods

  • FromFile: Creates an Image from the specified file
  • Fix: Returns integer form fraction values

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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Md. Marufuzzaman


Member
I've been a programmer since mid 1999's using languages like assembler, C/C++, PL/I.

However I'm specialized in databases and database modeling. Mostly I have used products, SQL Server (from version 7.0), DB2 (nowadays an IBM product).

For the past 6 years my main concerns have been dealing with different business processes and how to create software to implement them and consulting different areas on database management and database oriented software design.
Location: Bangladesh Bangladesh

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralThanks Pinmembervickypamgmail23:13 28 Jul '09  
GeneralRe: Thanks PingroupMd. Marufuzzaman3:11 29 Jul '09  
GeneralCan it resize a gif image with multi frames? Pinmembertaihip22:59 9 Jun '09  
GeneralA generic error occurred in GDI+ PinmemberMember 344752220:39 9 Jun '09  
GeneralRe: A generic error occurred in GDI+ PinmemberMember 344752222:21 9 Jun '09  
GeneralRe: A generic error occurred in GDI+ Pinmemberaa_zz22:21 9 Jul '09  
GeneralMy vote of 1 PinmemberNagy Vilmos3:01 9 Jun '09  
GeneralRe: My vote of 1 PinmemberMember 344752221:44 9 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jun 2009
Editor: Deeksha Shenoy
Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project