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.
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:
string that contains the image source file path
string that contains the destination file path
integer value for the new image width
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 (
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)
Dim imgOutput As New Bitmap(objImage, intWidth, intHeight)
Dim imgFormat = objImage.RawFormat
objImage = Nothing
If strImageSrcPath = strImageDesPath Then System.IO.File.Delete(strImageSrcPath)
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.
RawFormat: Gets the file format of this image
FromFile: Creates an Image from the specified file
Fix: Returns integer form fraction values
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.
This is a very simple and easy way. I hope that it might be helpful to you. Enjoy!
He is the founder & CEO of MNH Technologies and working for urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc. He has over ten years of professional experiences in design and developing Client-Server, Multi-Tier, Database, Web based business software solutions, Enterprise Applications, API, WebAPI, Google Analytics implementation, Add-In, Documentation & Technical Writing etc for Windows / Mac using Microsoft SQL Server, Oracle, MySql, PS, C#, VB.NET, ASP.NET, PHP, RoR, Visual Basic etc. He has also more than two years experience in Mobile-VAS (Platform Development).
He worked for various software development & technology consulting. His core focus on technologies to create dynamic data-driven systems that add value to your business and dynamic technology consulting that builds advanced solutions for the industries across the various vertices.
He also work as a Solution Architect at Dhrupadi Techno Consortium Limited (DTCL) and responsible for analyzing business requirements and offered optimum solutions (multiple options), which would address all current requirements, provide flexibility for future growth and allow smooth transition between old system and new system.
He graduated with honors from The University of Asia Pacific, in Computer Science and Engineering. He was awarded as “Most Valuable Professional” (MVP) at 2010 and 2011 by CodeProject.com and also selected as a Mentor of CodeProject.com
Specialties: Software Development Management, System Integration, Data Warehouse Architecture, Virtualization.