Reputation: 1746
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()
Upvotes: 0
Views: 944
Reputation: 21712
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.
Upvotes: 2
Reputation: 50712
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??
Upvotes: 0