Forum Moderators: open

Message Too Old, No Replies

Resizing images with System.Drawing.Bitmap

Can you help me troubleshoot an invalid parameter error?

         

ruug

5:30 pm on Jul 11, 2007 (gmt 0)

10+ Year Member



Hello. I've got this script that resizes a JPG that has just been uploaded to my website.

I am using the System.Drawing.Bitmap class and the constructor that Microsoft defines as


Visual Basic (Usage)
Dim original As Image
Dim width As Integer
Dim height As Integer

Dim instance As New Bitmap(original, width, height)

I've been searching all morning for a similar documented issue with no luck. Here is my code. I've snipped some irrelevant lines and all URLs.

Public Sub Page_Load( sender As Object, e As EventArgs )

'Read in the image filename to create a thumbnail of
dim imageUrl as string, vid as string, picCount as string
imageUrl = Request.QueryString("imageUrl")
vid = Request.QueryString("vid")
picCount = request.querystring("picCount")

'Add on the appropriate directory
imageUrl = "/tempPhotos/" & imageUrl

Dim originalImage As New System.Drawing.Bitmap( Server.MapPath( imageUrl ) )

dim maxImageWidth as integer
dim maxImageHeight as integer
dim newImageWidth as integer
dim newImageHeight as integer
dim originalWidth as integer
dim originalHeight as integer

maxImageWidth = 480
maxImageHeight = 360

originalWidth = originalImage.Width
originalHeight = originalImage.Height

if ( originalWidth > maxImageWidth ) or ( originalHeight > maxImageHeight ) Then
If ( originalWidth / maxImageWidth ) > ( originalHeight / maxImageHeight ) Then
newImageHeight = Math.Round( originalHeight * ( maxImageWidth / originalWidth ), 0 )
newImageWidth = maxImageWidth
Else
newImageWidth = Math.Round( originalWidth * ( maxImageHeight / originalHeight ), 0 )
newImageHeight = maxImageHeight
End If
end if

response.write( "width: " & newImageWidth & "<br>height: " & newImageHeight )

Dim newImage As New System.Drawing.Bitmap( originalImage, newImageWidth, newImageHeight )
Dim newPhotoName = "E:/someDirectoryOnMyServer/" & vid & "-" & picCount & ".jpg"

'save our resized image into the tempPhotos folder under a new name
newImage.Save( newPhotoName, System.Drawing.Imaging.ImageFormat.Jpeg )

'some irrelevant code i snipped out

End Sub

And here's the error that is produced:


Exception Details: System.ArgumentException: Invalid parameter used.

Source Error:

Line 53: response.write( "width: " & newImageWidth & "<br>height: " & newImageHeight )
Line 54:
Line 55: Dim newImage As New System.Drawing.Bitmap( originalImage, newImageWidth, newImageHeight )
Line 56: Dim newPhotoName = "E:/Inetpub/Motoverse/tempPhotos/" & vin & "-" & picCount & ".jpg"
Line 57:

Source File: E:\Inetpub\Motoverse\resizeImage.aspx Line: 55

Stack Trace:

[ArgumentException: Invalid parameter used.]
System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format) +113
System.Drawing.Bitmap..ctor(Int32 width, Int32 height) +14
System.Drawing.Bitmap..ctor(Image original, Int32 width, Int32 height) +35
ASP.resizeImage_aspx.Page_Load(Object sender, EventArgs e) in E:\Inetpub\Motoverse\resizeImage.aspx:55
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750

I don't know what could be wrong unless the Bitmap class won't accept an image of its own type for the Image parameter. I don't know much about reading the stack trace other than the constructor I'm trying to use is in there "System.Drawing.Bitmap..ctor(Image original, Int32 width, Int32 height) +35"

Any assistance will be greatly appreciated!

andyll

5:01 am on Jul 12, 2007 (gmt 0)

10+ Year Member



I don't know what could be wrong unless the Bitmap class won't accept an image of its own type for the Image parameter. .

I don't think so... the actual call it failed on does not include that paramter.

If 'originalWidth' is 0 and 'originalHeight <= maxImageHeight'
Then newImageWidth will equal 0

I assume passing a 0 width or height to an image function is invalid.

Andy