Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > how to convert httpPostedFile to and Drawing.Image (save a thumbnail)

Reply
Thread Tools

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

 
 
NATO24
Guest
Posts: n/a
 
      03-25-2005
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
 
Reply With Quote
 
 
 
 
Jason Bentley
Guest
Posts: n/a
 
      03-25-2005
Nato, post your code. This type of problem is hard to troubleshoot
without seeing the suspect code.

Jason Bentley
http://geekswithblogs.net/jbentley

 
Reply With Quote
 
 
 
 
Nathan
Guest
Posts: n/a
 
      03-26-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HttpFileCollection and HttpPostedFile Jennifer Mathews ASP .Net 2 11-13-2009 09:13 PM
HttpPostedFile.SaveAs Al Smith ASP .Net 8 09-01-2004 02:08 AM
HttpPostedFile problem huan ASP .Net 3 01-05-2004 01:54 AM
HttpPostedFile ASP .Net 4 11-11-2003 05:22 PM
HttpPostedFile.SaveAs() permission error Jeff ASP .Net 1 10-27-2003 08:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57