Introduction
This article will show you on how to resize an image keepeing the best resulation quality.
Background
This is a most common fearutes to add image / signature in any web/windows application. In my project I need to give an interface where the user upload there images and the system has the ability to resizd the image accordingly.
Using the code
Most of the time developers face to resize an uploded images runtime. In this article I try to resize an image runtime with keeping the best quality, for this purpose I use the fetures of .Net framework 3.5 like (System.Drawing, IO). The sample code is given below:
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 Return "Err."
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)
imgOutput.Save(strImageDesPath, imgFormat)
imgOutput.Dispose()
Return strImageDesPath
End Function
Function Detail:
Fix : Return the integer portion of a number.
FromFile:Creates an System.Drawing.Image from the specified file.
Input Valus # A string that contains the name of the file from which to create the System.Drawing.Image.
Return Values # The System.Drawing.Image this method creates.