20
Jul

Generate thumbnail for image on fly

   Posted by: admin   in .Net Emperor

Hello Friends,

Now a days in any web appication comman request made by clients is the generates a thumbnail for any product image or user profile image so here is the solution for the generates thumbnail on fly when admin or user add any image.

Private Function GetThumbnailView(ByVal originalImagePath As String, ByVal height As Integer, ByVal width As Integer) As String
‘Consider Image is stored at path like “ProductImage\\Product1.jpg”

‘Now we have created one another folder ProductThumbnail to store thumbnail image of product.
‘So let name of image be same, just change the FolderName while storing image.
Dim thumbnailImagePath As String = originalImagePath.Replace(”ProductImage”, “ProductThumbnail”)
‘If thumbnail Image is not available, generate it.
If Not System.IO.File.Exists(Server.MapPath(thumbnailImagePath)) Then
Dim imThumbnailImage As System.Drawing.Image
Dim OriginalImage As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(originalImagePath))
imThumbnailImage = OriginalImage.GetThumbnailImage(width, height, New System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero)
imThumbnailImage.Save(Server.MapPath(thumbnailImagePath), System.Drawing.Imaging.ImageFormat.Jpeg)

imThumbnailImage.Dispose()
OriginalImage.Dispose()
End If
Return thumbnailImagePath
End Function

Public Function ThumbnailCallback() As Boolean
Return False
End Function

Call this function like

GetThumbnailView(”ProductImage\Product1.jpg”, 100, 100)

Enjoy Friends.

Tags: , , ,

This entry was posted on Monday, July 20th, 2009 at 3:10 am and is filed under .Net Emperor. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment