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

N

NATO24

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
 
N

Nathan

Jason said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top