I came across some code out in the wild wild internet that allowed me to automagically create a thumbnail image from an image being uploaded by a user. I use it in an EditRecord page where I give the user a FileUpload control, allowing them to upload files and images. I wanted a way to display a thumbnail image on the ShowRecord page if they uploaded an image, but I didn't want the user to have to download the entire image (which could be megabytes in size), and have it resized on the screen. I thought I'd share the magic.
The table in the database that the record with the image is being saved to has a Screenshot column and a Thumbnail column.
If Me.Screenshot.FileName <> "" Then
Dim FileExtension As String = Me.Screenshot.FileName.Substring(Me.Screenshot.FileName.Length - 3, 3).ToUpper
'Determine whether file being uploaded is an image.
If FileExtension = "BMP" Or FileExtension = "JPG" Or FileExtension = "GIF" Or FileExtension = "PNG" Then 'File is an image, parse for thumbnail
'Create Thumbnail variable
Dim Thumbnail As System.Drawing.Image = Nothing
'Populate with current Screenshot image
Thumbnail = Drawing.Image.FromStream(Me.Screenshot.PostedFile.InputStream())
'Convert thumbnail to new size
Thumbnail = Thumbnail.GetThumbnailImage(CInt(Thumbnail.Width / 5), CInt(Thumbnail.Height / 5), Nothing, New IntPtr())
'Convert Image to byte array
Dim imgByteArray As Byte() = Nothing
Dim imgMemoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
imgByteArray = imgMemoryStream.GetBuffer()
'Save thumbnail to record
Report.ThumbnailImage = imgByteArray
Don't forget to make sure your Save button has its Postback property set to true, or it won't work!