By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,214 Members | 1,971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,214 IT Pros & Developers. It's quick & easy.

how to convert httpPostedFile to and Drawing.Image (save a thumbnail)

P: n/a

I am trying to write a sub routine that I can pass a posted (image) file
to. That routine will save the original file, then create a thumbnail
and save it. When I try to create the image with the posted file, I get
an error saying file type HttpPostedFile cannot be converted to an
Image. When I save the original and try and use the FromFile method to
create the thumbnail, it says that the file is already in use.

Any ideas?

Thanks for your help!

Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Nato, post your code. This type of problem is hard to troubleshoot
without seeing the suspect code.

Jason Bentley

Nov 19 '05 #2

P: n/a
Jason Bentley wrote:
Nato, post your code. This type of problem is hard to troubleshoot
without seeing the suspect code.

Jason Bentley

Its not pretty, but here it is...

If Not (myFile.PostedFile Is Nothing) Then
Dim objImage As System.Drawing.Image
Dim intHeight, intWidth As Integer
Dim strFilename As String = Path.GetFileName(myFile.PostedFile.Filename)
Dim strPath As String = Server.MapPath("/images/")

'ensure image is jpg format
If LCase(Right(strFilename,3)) = "jpg" Or LCase(Right(strFilename,3)) =
"peg" Then

'ensure image is under 1 mb
If myFile.PostedFile.ContentLength < 1000000 Then '1MB

'save original image
myFile.PostedFile.SaveAs(strPath & strFilename)

objImage.FromFile(".." & strPath & strFilename)

If objImage.Height > objImage.Width Then
' Work out a proportionate width from height
intWidth = objImage.Width / (objImage.Height / intHeight)
'work out a proportionate height from width
intHeight = objImage.Height / (objImage.Width / intWidth)
End If

Dim strPathTN As String = strPath & "tn_" & strFilename
objImage.GetThumbnailImage(intWidth, intHeight, Nothing, System.IntPtr.Zero)

objImage.Save(strPathTN, ImageFormat.Jpeg)


'Message.Text = "Image Added Successfully."
img_staff.ImageURL = "../images/tn_" & strFilename
'notify user image is too large
'Message.Text = "Image is too large"
End If
Catch exc As Exception
Message.Text = exc.ToString()
End Try
'notify user that image is not jpeg format
'Message.Text = "Please ensure image is jpg format."
Exit Sub
End If
End If

End Sub
Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.