Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some images fails to load on Windows Server 2008

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).

like image 322
Guffa Avatar asked Apr 15 '10 08:04

Guffa


1 Answers

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);
  }
}
like image 167
Guffa Avatar answered Oct 16 '22 00:10

Guffa