Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Image Resize or create thumb image

Posted by Viral Sarvaiya on August 4, 2009

The project shows the resizing of the image, means create the thumb file of the actual image, here i show how to create the thumb as well as Medium sized file.

first create the class file named “GeneralMethods.vb”

Public Class GeneralMethods
Public Shared Function CreateThumbImage(ByVal imgPhoto As Image, ByVal Width As Integer, ByVal Height As Integer) As Image
Dim bmpOut As Bitmap = Nothing
Try
Dim oBMP As New Bitmap(imgPhoto)
Dim oFormat As Imaging.ImageFormat = oBMP.RawFormat
Dim NewWidth As Integer = 0
Dim NewHeight As Integer = 0

'*** If the image is smaller than a thumbnail just return it

If oBMP.Width <= Width AndAlso oBMP.Height <= Height Then
Return imgPhoto
End If

Dim per As Double
If oBMP.Width > Width Or oBMP.Height > Height Then
If oBMP.Width > oBMP.Height Then
per = (100 * Width) / oBMP.Width
NewWidth = (oBMP.Width * per) / 100
NewHeight = (oBMP.Height * per) / 100
Else
per = (100 * Height) / oBMP.Height
NewWidth = (oBMP.Width * per) / 100
NewHeight = (oBMP.Height * per) / 100
End If

If NewHeight > Height Or NewWidth > Width Then
If NewWidth > NewHeight Then
per = (100 * Width) / NewWidth
Else
per = (100 * Height) / NewHeight
End If
NewWidth = (NewWidth * per) / 100
NewHeight = (NewHeight * per) / 100
End If

End If

bmpOut = New Bitmap(NewWidth, NewHeight)

Dim g As Graphics = Graphics.FromImage(bmpOut)

g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
g.InterpolationMode = Drawing2D.InterpolationMode.High
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality

Dim codec As Drawing.Imaging.ImageCodecInfo = Drawing.Imaging.ImageCodecInfo.GetImageEncoders(1)
Dim eParams As Drawing.Imaging.EncoderParameter = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 1)

g.FillRectangle(Brushes.White, 0, 0, NewWidth, NewHeight)

g.DrawImage(oBMP, 0, 0, NewWidth, NewHeight)

oBMP.Dispose()

Catch
Return Nothing
End Try
Return bmpOut
End Function

Imports this 2 namesapces,
Imports System.IO
Imports System.Drawing

in the html side, take a fileupload control named “file1” and one button.
on the click of the button write the following code


If file1.HasFile Then
extension = Path.GetExtension(file1.PostedFile.FileName).ToLower()
SyncLock (objLock)
'get the max id from the database
maxId = (objclass.GetMaxId())
filename = maxId & extension

strImagePath = rootPath & ConfigurationManager.AppSettings("ACTUAL_PATH") & filename
End SyncLock
If Not Directory.Exists(rootPath & ConfigurationManager.AppSettings("ACTUAL_PATH")) Then
Directory.CreateDirectory(rootPath & ConfigurationManager.AppSettings("ACTUAL_PATH"))
End If

objprice.photograph = filename

file1.PostedFile.SaveAs(strImagePath)

If File.Exists(strImagePath) Then
Dim actualImage As Drawing.Image
Dim mediumImage As Drawing.Image = Nothing
Dim thumbImage As Drawing.Image = Nothing

actualImage = Drawing.Image.FromFile(strImagePath)

'------------- Medium Image --------------------
Dim mediumWidth As Integer = ConfigurationManager.AppSettings("MEDIUM_WIDTH")
Dim mediumHeight As Integer = ConfigurationManager.AppSettings("MEDIUM_HEIGHT")
mediumImage = GeneralMethods.CreateThumbImage(actualImage, mediumWidth, mediumHeight)

If mediumImage.Width > mediumWidth Then
mediumImage = mediumImage.GetThumbnailImage(mediumWidth, mediumImage.Height, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If mediumImage.Height > mediumHeight Then
mediumImage = mediumImage.GetThumbnailImage(mediumImage.Width, mediumHeight, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If mediumImage IsNot Nothing Then
mediumImage.Save(rootPath & ConfigurationManager.AppSettings("MEDIUM_PATH") & filename)
End If
mediumImage.Dispose()
actualImage.Dispose()
'-------------  End Medium  --------------------

actualImage = Drawing.Image.FromFile(strImagePath)
'--------------- Thumb Image -----------------
Dim thumbWidth As Integer = ConfigurationManager.AppSettings("THUMB_WIDTH")
Dim thumbHeight As Integer = ConfigurationManager.AppSettings("THUMB_HEIGHT")
thumbImage = GeneralMethods.CreateThumbImage(actualImage, thumbWidth, thumbHeight)

If thumbImage.Width > thumbWidth Then
thumbImage = thumbImage.GetThumbnailImage(thumbWidth, thumbImage.Height, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If thumbImage.Height > thumbHeight Then
thumbImage = thumbImage.GetThumbnailImage(thumbImage.Width, thumbHeight, New Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
End If

If thumbImage IsNot Nothing Then
thumbImage.Save(rootPath & ConfigurationManager.AppSettings("THUMB_PATH") & filename)
End If
thumbImage.Dispose()
'------------- End Thumb Image -----------------

actualImage.Dispose()
End If

End If
Private Function ThumbnailCallback() As Boolean
Return False
End Function

according to the requirements set the application setting of the web.config.

hope this will help you

enjoy coding…..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: