8

I have an application running on a Windows Server 2008, that is processing uploaded images. Currently it successfully processes about 8000 images per day, creating 11 different sizes of each image.

The problem that I have is that sometimes the application fails to load some images, I get the error "System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.".

The upload only accepts files with a JPEG extension (jpg/jpeg/jpe) or with a JPEG MIME type, and from what I can tell those images are really JPEG images. If I look at the image file in windows explorer on the server, it can successfully extract the thumbnail from the file, but if I try to open it, I get the error message "This is not a valid bitmap file, or it's format is not currently supported." from Paint.

If I copy the image to my own computer, running Windows 7, there is no problem opening the image. It works in Paint, Windows Photo Viewer, Adobe Bridge, and Photoshop. If I try to load the image using Image.FromStream the same way as in the application running on the server, it loads just fine. (I have copied the file back to the server, and it still doesn't work, so there is nothing in the copying process that changes it.)

When I look at the image information in Bridge, I see that the images are created using Picasa 3.0, but other than that I can't see anything special about them. I haven't yet found anyone having the same problem, or any known problems like this with the Picasa application.

Has anyone had any similar problem, or know if there is something special about images created using Picasa? Is there any image codec that needs installing on the server to handle all kinds of JPEG images?

Here is an example of an image that doesn't load on the server: gdi-example.jpg (192 kB).

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Interesting. I can confirm this using your example image on Windows Server 2008 and 2003 (although the Picture Viewer gives me a 1x1 pixel image in 2003). Can't see anything wrong with the image. On Windows 7, it opens fine. This may be some security related GDI patch or something, but I have no idea what to do. – Pekka Apr 15 '10 at 09:25
  • @Pekka: Thanks for confirming it, then I know at least that the problem is likely to be related to the OS version, and not just this server. – Guffa Apr 15 '10 at 10:04

2 Answers2

2

From Experts exchange I got an example using a BitmapImage object to load the image and resave it to a MemoryStream. The BitmapImage can for some reason load the images that the Bitmap object can't.

I also had to load the data from the file and feed it to the BitmapImage as a MemoryStream, so that it wouldn't lock the file.

So, this is the final code (sans some logging) that I use now:

using WpfImaging = System.Windows.Media.Imaging;
...

byte[] data = File.ReadAllBytes(FileName);

Image master;
using (MemoryStream source = new MemoryStream(data)) {
  var img = new WpfImaging.BitmapImage();
  img.BeginInit();
  img.StreamSource = source;
  img.EndInit();
  WpfImaging.BmpBitmapEncoder encoder = new WpfImaging.BmpBitmapEncoder();
  using (MemoryStream m = new MemoryStream()) {
    encoder.Frames.Add(WpfImaging.BitmapFrame.Create(img));
    encoder.Save(m);
    master = new Bitmap(m);
  }
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

I think it must be something in the EXIF header info generated by Picasa. I was having the same problem - processing thousands of images, but occasionally a few don't want to process. I used the cloning fix here: How can I get .Net to save this image?

Community
  • 1
  • 1
user735232
  • 181
  • 2
  • 6