Labels:

In this article you can understand how to create thumbnails from images at time of Upload using ASP.NET.I create a small utility class that can convert and image into thumbnails.

Imports
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D


Public Class Image_Process
Public Function CreateThumbnail(ByVal lcFilename As String, ByVal targetSize As Integer) As Bitmap
Try
Dim loBMP As Bitmap = New Bitmap(lcFilename)
Dim loFormat As ImageFormat = loBMP.RawFormat
Dim newSize As Size = CalculateDimensions(loBMP.Size, targetSize)
Dim bmpOut As Bitmap = Nothing
bmpOut = New Bitmap(newSize.Width, newSize.Height)
Dim canvas As Graphics = Graphics.FromImage(bmpOut)
canvas.SmoothingMode = SmoothingMode.AntiAlias
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality
canvas.DrawImage(loBMP, New Rectangle(New Point(0, 0), newSize))
Return bmpOut
Catch ex As Exception
Return Nothing
End Try
End Function

Private Function CalculateDimensions(ByVal oldSize As Size, ByVal targetSize As Integer) As Size
Dim newSize As Size = New Size()
If oldSize.Height > oldSize.Width Then
newSize.Width = CInt(oldSize.Width * (CType(targetSize, Single) / CType(oldSize.Height, Single)))
newSize.Height = targetSize
Else
newSize.Width = targetSize
newSize.Height = CInt(oldSize.Height * (CType(targetSize, Single) / CType(oldSize.Width, Single)))
End If
Return newSize
End Function Microsoft.VisualBasic


End Class

The class define above can be use to covert an image into a thumbnail to be display on the list of images.
There is a method CreateThumbnail which can take two parameters and send the output in the form of Bitmap that you can save on your disk.

Below i create a sample program in which i used the above code.

Try
Dim strPath As String = Server.MapPath(Request.ApplicationPath) & "/Images/Original"
Dim filename As String = Guid.NewGuid().ToString().Substring(0, 10) & "" & video_upload.PostedFile.FileName.Remove(0, video_upload.PostedFile.FileName.LastIndexOf("."))' Dim filename As String = video_upload.PostedFile.FileName.Remove(0, video_upload.PostedFile.FileName.LastIndexOf("\") + 1)If Not IsImageFile(filename) Then
ShowMessage("This format is not supported")
Exit Sub
End If
If File.Exists(strPath & "/" & filename) Then
ShowMessage("File already exist, please rename it first")
Exit Sub
End Ifvideo_upload.PostedFile.SaveAs(strPath & "/Photos/" & filename)
video_upload.PostedFile.SaveAs(strPath & "/SnapShots/" & filename)
Dim orignalfilename As String = strPath & "/Photos/" & filename
Dim thumbfilename As String = strPath & "/SnapShots/" & filename

Here i use the function create thumbnail, and pass filename originalfilename that
convert originalfilename into 125px resolution

Dim mp As Bitmap = _img_process.CreateThumbnail(orignalfilename, 125)
If mp Is Nothing Then
ShowMessage("No image generated")
Exit Sub
End If
mp.Save(thumbfilename)

This code will provide you a clear understanding how you can manipulate images at time of upload using ASP.NET