Monday, December 27, 2010

Generate thumbnail image in asp.net

Dear All,
Many times we require thumbnail image on the fly.
ASP.Net provides an easy way by which we can create thumbnail. I am showing basic steps by which we can create thumbnail.
Asp.net provides “System.Drawing.Image “namespace which having property “GetThumbnailImage” by which we can get Thumbnail image.
It’s a simplest way to get a thumbnail.
Below is the code written in VB.NET by which we upload an image, save image & create thumbnail and save thumbnail with thumbnail prefix.

Protected Sub Unnamed1_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
If Me.FileUpload1.FileName.Trim <> String.Empty AndAlso Me.FileUpload1.FileContent.Length > 0 Then
Dim strUploadPath As String = Server.MapPath("images")
Dim strFileName As String = Me.FileUpload1.FileName
FileUpload1.SaveAs(strUploadPath & "\" & strFileName)
Dim img1 As System.Drawing.Image = System.Drawing.Image.FromFile(strUploadPath & "\" & strFileName)
Dim thumbNail As System.Drawing.Image = img1.GetThumbnailImage(100, 100, Nothing, IntPtr.Zero)
thumbNail.Save(strUploadPath & "\thubmnail_" & strFileName)
Me.imgActual.ImageUrl = "~/images/" & strFileName
Me.imgThumb.ImageUrl = "~/images/" & "thubmnail_" & strFileName
End If
Catch ex As Exception

End Try
End Sub



Enjoy programming.

Thanks & Regards
Rajat Jaiswal