After reading the article on true image resizing on 4 Guys from Rolla.com, I wanted to create a function that I could reuse to create thumbnail images and resized images after an image upload.
The following function will create either a thumbnail of the original uploaded image or will resize the original uploaded image depending on the paramaters that are passed into the function.
The parameters for this function are as follows:
lsFilename: The filename of the uploaded file
lsImagePath: The complete path of the uploaded file
liResize: The pixel size the image should be resized to
liResizeType: If this equals 1 the image will be resized by width. If this equals 2 the image will be resised by height
lbCreateThumbNail: If True a new image will be saved ending with “_smail.jpg”
To use this function create a new class in your App_Code Folder and paste the following code into your new file. This will allow the class to be available via Intellisense in Visual Studio.
If anyone has any suggestions on how I could improve this code, let me know.
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class BMFuncLib
‘*********************************************************************
‘Description: Resizes original image or creates thumbnail.
‘*********************************************************************
Sub ResizeImage(ByVal lsFilename As String, ByVal lsImagePath As String, ByVal liResize As Integer, ByVal liResizeType As Integer, ByVal lbCreateThumbNail As Boolean)
Dim liWidth As Integer
Dim liHeight As Integer
Dim lbResize As Boolean = False
Dim loDummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
loDummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
Dim loFullSizeImg As System.Drawing.Image
loFullSizeImg = System.Drawing.Image.FromFile(lsImagePath + lsFilename)
Select Case liResizeType
‘Type 1 is resize by width
Case 1
liWidth = liResize
liHeight = (liWidth * loFullSizeImg.Height) / loFullSizeImg.Width
If loFullSizeImg.Width > liWidth Then
lbResize = True
End If
‘Type 2 is resize by height
Case 2
liHeight = liResize
liWidth = (liHeight * loFullSizeImg.Height) / loFullSizeImg.Height
If loFullSizeImg.Height > liHeight Then
lbResize = True
End If
End Select
Dim lsOutputFilename As String
Dim lsSplitlcFilename() As String
If lbResize Then
Dim loResizedImg As System.Drawing.Image
loResizedImg = loFullSizeImg.GetThumbnailImage(liWidth, liHeight, loDummyCallBack, IntPtr.Zero)
loFullSizeImg.Dispose()
lsSplitlcFilename = lsFilename.Split(”.”)
If lbCreateThumbNail Then
lsOutputFilename = lsImagePath + lsSplitlcFilename(0) + “_small.jpg”
Else
lsOutputFilename = lsImagePath + lsSplitlcFilename(0) + “.jpg”
Try
Dim loFileInfo As FileInfo = New FileInfo(lsOutputFilename)
If loFileInfo.Exists Then
File.Delete(lsOutputFilename)
Else
Throw New FileNotFoundException()
End If
Catch ex As FileNotFoundException
Catch ex As Exception
End Try
End If
loResizedImg.Save(lsOutputFilename, ImageFormat.Jpeg)
loResizedImg.Dispose()
Else
‘Create thumbnail of original size if resize not needed
If lbCreateThumbNail Then
lsSplitlcFilename = lsFilename.Split(”.”)
lsOutputFilename = lsImagePath + lsSplitlcFilename(0) + “_small.jpg”
loFullSizeImg.Save(lsOutputFilename, ImageFormat.Jpeg)
End If
loFullSizeImg.Dispose()
End If
End Sub
‘Helper function. Don’t need to know about this
Function ThumbnailCallback() As Boolean
Return False
End Function