467,876 Members | 1,190 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,876 developers. It's quick & easy.

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

Hello,

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!

Nathan
Nov 19 '05 #1
  • viewed: 6493
Share:
2 Replies
Nato, post your code. This type of problem is hard to troubleshoot
without seeing the suspect code.

Jason Bentley
http://geekswithblogs.net/jbentley

Nov 19 '05 #2
Jason Bentley wrote:
Nato, post your code. This type of problem is hard to troubleshoot
without seeing the suspect code.

Jason Bentley http://geekswithblogs.net/jbentley

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

Try
'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)
Else
'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)

objImage.Dispose()

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

End Sub
Thanks,
Nathan
Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Toby Mathews | last post: by
reply views Thread by Nicolas Guilhot | last post: by
8 posts views Thread by Chris Dewin | last post: by
1 post views Thread by Fritz Switzer | last post: by
4 posts views Thread by Harry Hudini | last post: by
7 posts views Thread by Scott Schluer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.