Click here to Skip to main content
Click here to Skip to main content

An Easy Way to Resize an Image

, 9 Jun 2009
Rate this:
Please Sign up or sign in to vote.
This article will show you how to resize an image keeping with the best graphics quality.

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)

Share

About the Author

Md. Marufuzzaman
CEO
Bangladesh Bangladesh
A highly experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by codeproject.com and also selected as a mentor of codeproject.com. Published over 100 technical articles in various software development resource sites (i.e., codeprojetc.com, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.
 
Over ten years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.
 
An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint. Believes in transparency, commitment and teamwork.
 
Expertise: Software/Solution Architect, SaaS platform base application, Large scale win32/web based business software solutions, enterprise applications, integration, etc.
 
Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12 , Oracle version 10/11, MySql version 5.1, 5.5, PS2, Visual C#, R, VB.NET, ASP.NET, PHP, API, MVC, WebAPI , Add-In Visual Basic etc.,. I have also more than two years’ of strong experience in mobile-VAS (platform development).
 
Points of Interest: Technology and research & development especially focused on business functionalities and social business areas as well, few stuff including:
 
1.R&D on new techniques as required to increase business revenue.
2.Urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc., using innovative technical solutions.
3.Research and innovative product development.
4.Etc.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 1 PinmemberNagy Vilmos9-Jun-09 2:01 
GeneralRe: My vote of 1 PinmemberMember 34475229-Jun-09 20:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 10 Jun 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid