473,698 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
2 7228
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.PostedF ile Is Nothing) Then
Dim objImage As System.Drawing. Image
Dim intHeight, intWidth As Integer
Dim strFilename As String = Path.GetFileNam e(myFile.Posted File.Filename)
Dim strPath As String = Server.MapPath( "/images/")

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

Try
'ensure image is under 1 mb
If myFile.PostedFi le.ContentLengt h < 1000000 Then '1MB

'save original image
myFile.PostedFi le.SaveAs(strPa th & strFilename)

objImage.FromFi le(".." & strPath & strFilename)

If objImage.Height > objImage.Width Then
' Work out a proportionate width from height
intWidth = objImage.Width / (objImage.Heigh t / 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.GetThu mbnailImage(int Width, intHeight, Nothing, System.IntPtr.Z ero)

objImage.Save(s trPathTN, ImageFormat.Jpe g)

objImage.Dispos e()

'Message.Text = "Image Added Successfully."
img_staff.Image URL = "../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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
14243
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method, then use the GetThumbnailImage method to create a second Image. This second one is the one I want to get a FileStream from, so that I can then use its Handle to use Response.WriteFile to output the thumbnail on my ASP.Net page. Any help would...
0
17976
by: Nicolas Guilhot | last post by:
Hi all ! I have a multi-page Tiff image file that I want to convert to PDF. To do so I am using iText library. The conversion is working, but the code execution is very different according to wich iTextSharp.text.Image.getInstance(...) signature I am using : - using code 1 below, the conversion is fast enough but the resulting PDF file is too big (1 817ko sample Tiff file is converted in less than 30 seconds to a 2 764ko PDF file) -...
8
4813
by: Chris Dewin | last post by:
Hi. I run a website for my band, and the other guys want an image gallery. I'm thinking it would be nice and easy, if we could just upload a jpg into a dir called "gallery/". When the client clicks the "gallery" link, a cgi script could search the gallery/ dir, and create thumbnails of any jpeg images that don't already have a thumbnail associated with them. The script could then generate a page of clickable thumbnails. A few questions:
1
50532
by: Fritz Switzer | last post by:
With two controls a PictureBox and another "PictureBox like" , Pic2, a control from a dll, I'm stuck on cannot implicity convert System.Drawing.Image to System.Drawing.Bitmap error. How do I convert the following code so that the Pic2 can accept the Image from the stream where it wants Picture property not and Image property. Here is a snippet, I'm trying to avoid writing to disk.: Image i = Pic1.Image;
4
2414
by: Harry Hudini | last post by:
Hi, I need to run exactly this code in an asp.net file when someone uploads an image, but i dont know C# and im having real issues converting it. If anyone can, could you convert it to VB.net for me to use ? Loads of karma in it for you :) <%@ Import Namespace="System.Collections" %>
6
3997
by: hb | last post by:
Hi, Would you please give me some idea to convert/decode a Base 64 encoded GIF image string to a *.gif file in ASP.Net? Thank you hb
1
5410
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a web form to upload a TIFF image (scanned images) and convert it to a JPEG before I insert it into SQL Server. The file upload part works great, but I can;t convert the image. I certainly don't want to upload it the the server filesystem, convert...
7
3397
by: Scott Schluer | last post by:
Is there a way to use the Image class to convert a color photo (GIF or JPEG) to a B&W photo? Thanks, Scott
3
3102
by: neovantage | last post by:
Hey all, i have created image by mixing logo and the original image. It create image and show in the same window. I have done 3 steps for this. First i upload an image to my server by using move_upload_file, then i resize it using class "thumbnail.class.php" My next step was to embed company logo in it(which i already done) and save new embedded logo image to the path where i first upload the original image i mean i want to overwrite...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.