tags:

views:

242

answers:

2

I am trying to re-size uploaded images. From what I have found online, the way to do this is to create a new image the size that you want it to be and then use Graphics to draw the image to a smaller image. The seems to work for every image that is uploaded except for images from a Nikon D90 camera. Every time I attempt to upload and re-size on of those images I get an OutOfMemoryException at the line shown below. Can anyone tell me what I'm doing wrong.

'Create the new image as a blank bitmap
Dim resized As Image = New Bitmap(newWidth, newHeight)

'Create a new graphics object from the new image
Dim g As Graphics = Graphics.FromImage(resized)

'Resize graphics object to fit onto the resized image
g.DrawImage(originalImage, New Rectangle(0, 0, resized.Width, resized.Height))  <-- Exception

g.Dispose()
A: 

I think this code will works fine if the variables resized and image are set correctly.

Do you have a zero size imace or rectangle??

ArsenMkrt
This is what I thought at first, but I have verified that new image width and height are not zero. Other images that I have uploaded work fine.
BLeB
+1  A: 

How are you loading the image? System.Drawing.Image.FromFile? According to the .Net Developer Center, this throws OutOfMemoryException if the image file is corrupt or in a format GDI+ does not recognize.

Can you open the image in an image editing program like Paint.Net? If so, save it as JPEG or something and try to open the saved program in your application.

If this fixes it, the original image has some kind of corruption or format that an image editor can handle but DrawImage cannot. You will have to pass the broken images through an image editor to fix them. You will have to find a image editing app with a command-line or scripting interface that you could call from your app.

Dour High Arch
I am using FromFile. I get the same error with FromStream, but I'm guessing that the reason is the same. The image will open in Paint.Net and if I re-size it in Paint.Net and then upload it they work just fine. This seems like an unnecessary step for my end-user. What is involved with the BinaryReader solution?
BLeB
Also, the exception happens on DrawImage not FromFile
BLeB
Try simply opening the image in Paint.Net and resaving it. You don't want the end user to do this, you will have to do it from your app. While the DrawImage docs do not say so, I suspect it has the same problem that FromFile does.
Dour High Arch
Also, my "BinaryReader" suggestion was to try reading the JPEG original as a binary file and regenerating it. Inspecting the JPEG spec shows this is an insane amount of work, you want to use an existing app to do this.
Dour High Arch