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.

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.

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.
I try your image,  and i get error on load image from file.

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

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

This works ok, but it requires PresentationCore reference.
Imports System.Windows.Media.Imaging
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Note I try also Load an Save the image sample with IrfanView.
The saved image works fine with  image.FromFile
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?

    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
    Me.BackgroundImage = New Bitmap(ms)

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.
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.StreamSource = source;
  WpfImaging.BmpBitmapEncoder encoder = new WpfImaging.BmpBitmapEncoder();
  using (MemoryStream m = new MemoryStream()) {
    master = new Bitmap(m);

Open in new window

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.