Link to home
Start Free TrialLog in
Avatar of Göran Andersson
Göran AnderssonFlag for Sweden

asked on

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?

Attached is an example of an image that doesn't load on the server.

gdi-example.jpg
Avatar of sufianmehmood
sufianmehmood
Flag of Pakistan image

Avatar of Göran Andersson

ASKER

sufianmehmood, thanks for the reply. However, it's not as simple as any of the answers in the threads that you linked to. It's not a permission problem, as I load the data into a byte array before starting the image processing, and use a MemoryStream to read from the byte array. As it processes so many images and creates several versions of each image, I am very careful to dispose of everything correctly.

As it's not only my application that fails to load the images, that would rule out that it's a problem with the code. The problem doesn't occur for all images or just some random images, it's for a few specific images. One of those specific images is attached to the question.

Note that while the question says "beginner on this subject", this is not correct. I could not change that when posting the question, but I would have chosen "advanced" or "guru" to correctly describe my experience level.
I try your image,  and i get error on load image from file.

I search (google) for "FFD8" check netframework images.

http://stackoverflow.com/questions/210650/validate-image-from-file-in-c

I Try  
   Dim bmp1 As New BitmapImage(New Uri(fileName))

This works ok, but it requires PresentationCore reference.
Imports System.Windows.Media.Imaging
Note I try also Load an Save the image sample with IrfanView.
The saved image works fine with  image.FromFile
x77, that's some progress, actually.

Checking that it's a JPEG image is not the problem, I actually only care if I can load the image or not. As the problem images really are JPEG images, they would not even get caught in a file header analysis.

Your suggestion to use BitmapImage to load the image actually works on the server, for some reason. However, as it's a XAML control it's not really an ideal solution for my application, which is a plain windows console application. I fear that I would have to rewrite it as a WPF application for it to handle XAML components without leaking memory.

Is there any way to load the image the same way that the BitmapImage control does it, without using the XAML control itself?
ASKER CERTIFIED SOLUTION
Avatar of x77
x77
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
x77, that looks promising.

I have done some performance testing, and there is no evidence of leaking memory eventhough the code uses the XAML control, so it seems to be usable.

I will implement this in the process, and see how it works out.
I have implemented this way of loading the image in the process, and it works fine.

However, there was a problem with the BitmapImage object locking the file, so I could not move a file away when I was done with it. I had to load the file into memory first, create a MemoryStream from it and set the StreamSource property of the BitmapImage.

So, this is how the final code ended up (sans some logging code):

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);
  }
}

Open in new window