16Feb Need to create thumbnails when uploading Images?
I had the need to upload images to my blog as well as create a thumbnail based on the image. I had the code written at www.jasncab.com, but it was in an EXE.
So I was left with converting it just a little.
First the code to upload the image:
The only real line here to pay attention to is
Call ProcessImage(filepath)
Private Sub cmdUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpload.Click Dim theFile As String = txtFile.PostedFile.FileName Dim filename As String filename = theFile.Substring(theFile.LastIndexOf("\") + 1)
Dim filepath As String = MapPath("db/images/") _ & filename Try txtFile.PostedFile.SaveAs(filepath) Call ProcessImage(filepath)
lblError.Text &= "Image Uploaded" Call SetupPage() Catch ex As Exception lblError.Text &= ex.Message End Try
End Sub
Then I needed to actually create the thumbnail. There a couple nuances here. You need a callback function delegate and I created a couple of functions to figure out the height and width for me as well based on 100 as the largest side.
deletegatetest is the only thing here to watch for. You need it for a callback, but it really doesnt need to do anything.
#Region "Imagestuff" Private Sub DeleteImage(ByVal strImage As String) If System.IO.File.Exists(MapPath("db/images/" & strImage)) Then System.IO.File.Delete(MapPath("db/images/" & strImage)) End If If System.IO.File.Exists(MapPath("db/images/thumbnails/" & strImage.Replace(".jpg", "_thumb.jpg"))) Then System.IO.File.Delete(MapPath("db/images/thumbnails/" & strImage.Replace(".jpg", "_thumb.jpg"))) End If End Sub Private Sub ProcessImage(ByVal strImage As String) Dim imgSource As System.Drawing.Image Dim imgOutput As System.Drawing.Image Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort Dim strDestDir As String
strDestDir = MapPath("db/images/thumbnails/") + strImage.Substring(strImage.LastIndexOf("\") + 1) lblError.Text &= strDestDir dummyCallBack = New _ System.Drawing.Image.GetThumbnailImageAbort(AddressOf delegatetest) Try imgSource = System.Drawing.Image.FromFile(strImage) imgOutput = imgSource.GetThumbnailImage(GetImageWidth(imgSource), GetImageHeight(imgSource), dummyCallBack, IntPtr.Zero)
imgOutput.Save(strDestDir.Replace(".jpg", "_Thumb.jpg"), Imaging.ImageFormat.Jpeg)
imgSource.Dispose() imgOutput.Dispose() Catch e As Exception 'I dont care. lblError.Text &= e.Message End Try imgSource = Nothing imgOutput = Nothing
End Sub Public Function delegatetest() As Boolean Return False End Function Public Function GetImageWidth(ByVal imgS As System.Drawing.Image) As Integer Dim aspR As Double If imgS.Width > imgS.Height Then GetImageWidth = 100 Else aspR = 100 / imgS.Height GetImageWidth = CType(imgS.Width * aspR, Integer) End If End Function Public Function GetImageHeight(ByVal imgS As System.Drawing.Image) As Integer Dim aspR As Double If imgS.Width > imgS.Height Then aspR = 100 / imgS.Width GetImageHeight = CType(imgS.Height * aspR, Integer) Else GetImageHeight = 100 End If End Function#End Region

