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
LVL 29
Göran AnderssonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sufianmehmoodCommented:
0
Göran AnderssonAuthor Commented:
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.
0
x77Commented:
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
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

x77Commented:
Note I try also Load an Save the image sample with IrfanView.
The saved image works fine with  image.FromFile
0
Göran AnderssonAuthor Commented:
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?
0
x77Commented:

    Dim fileName = "D:\Documents and Settings\U128728\Escritorio\gdi-example.jpg"
    Dim bmp1 As New BitmapImage(New Uri(fileName))

    'Creating a BitMap from a BitmapImage
    Dim ms As New MemoryStream, enc As New BmpBitmapEncoder
    enc.Frames.Add(BitmapFrame.Create(bmp1))
    enc.Save(ms)
    Me.BackgroundImage = New Bitmap(ms)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Göran AnderssonAuthor Commented:
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.
0
Göran AnderssonAuthor Commented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.