Listing 2: Sample Code: Here's the complete code-behind for the upload and thumbnail demo.
Imports System.Web
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Image
Imports System.Drawing.Imaging
Partial Class _Default
Inherits System.Web.UI.Page
Private Const imagePath As String = "~/Images/"
Private filename As String = ""
Private Const thumbnailMask As String = "thumbnail_{0}"
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
ProcessPostedFile()
End Sub
Private Sub ProcessPostedFile()
filename = Path.GetFileName(File1.PostedFile.FileName)
SaveUploadedFile(filename)
SaveThumbnail(filename)
SetPreviewImage(filename)
End Sub
Private Sub SaveUploadedFile(ByVal filename As String)
File1.PostedFile.SaveAs(MapPath(imagePath) + filename)
End Sub
Private Function Callback() As Boolean
Return False
End Function
Private Sub SaveThumbnail(ByVal filename As String)
Dim image As Image = _
image.FromStream(File1.PostedFile.InputStream)
Dim thumbnail As Image = image.GetThumbnailImage( _
100, 100, AddressOf Callback, IntPtr.Zero)
thumbnail.Save(MapPath(GetThumbnailPath(filename)))
End Sub
Private Function GetThumbnailPath(ByVal filename As String) As String
Return "~/Images/" + String.Format(thumbnailMask, filename)
End Function
End Class