tags:

views:

715

answers:

4

The following piece of source does run nicely with Windows up until vista. With Windows 7 (and the new .net 3.5) it always produces an out of memory exception, when I try to load a raw image file from my Nikon D90.
Some might say "loading nef's is not supported", but it did run nicely up until vista, only Windows 7 broke it, so I'd disagree.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace QuickImageLoader
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
      DialogResult res = openFileDialog1.ShowDialog();
      if (res == DialogResult.OK)
      {
        pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); // crash happens
        pictureBox1.Refresh();
      }
    }
  }
}

Download a sample nef file to reproduce error.

Is this a bug in Windows 7? In .net 3.5? Or is it something that should have never worked with XP/Vista?

[Update] Since a few people neither know nor read my introduction above: loading the nef like this does work on XP/Vista without installing the Nikon Raw Codec. And installing the codec does not solve the problem (folks, it got nothing to do with the codec sigh).

A: 

By itself, Image.FromFile does not support a lot of image types, but more can be installed.

So, did you install something into Vista that made this work? Like a Nikon Codec pack or something that would add that support?

And that error message means one of two:

  • The image file is not supported (which can, as pointed out above, be fixed by installing the appropriate codecs or whatnot)
  • The image file is indeed supported, but the file contents are corrupt, so some value inside that is used to allocate a memory struct is way beyond bounds (like an image that says it is 217273373 pixels wide)

I see from the comments that there is some discussion here. I do not know whether Microsoft explicitly pulled the format by themselves, or was forced to do so, but I can confirm that GDI+ does not support NEF on my Windows 7 installation, so regardless of whether XP or Vista supported it, and regardless of the reason for why it is no longer support, Windows 7 does not support NEF-loading in GDI+ out of the box.

So you need to install something.

Lasse V. Karlsen
I can confirm that it works out of the box on Vista. Just tried and I didn't install a raw codec.
Joey
The code does work without installing the Nikon Raw Codec on XP/Vista.
Sam
i am unconvinced by the down-votes; these posts contain useful information, even if it didnt work.
Pondidum
So, perhaps that is your answer... Perhaps support for that codec was not included in Windows 7. This happens frequently as licensing issues cause MS to pull native support for certain things. I would research the coded support differences between them.
Brian Rudolph
Well, I don't think wrong information is useful, rather the opposite. Try for yourself, you'll notice my example code and file will work with Vista/Xp and won't for Windows 7, which no part of the answer explains.
Sam
Brian, you might be right, I'd consider your information as a useful answer, but this was not in the answer - and SO does not allow me to take the downvote back just because some comments where added. :(
Sam
Oh, might I add, installing the Nikon Raw Codec does not resolve the issue.And of course GDI+ does not support display of NEF files. But it did support reading the metatags from NEF, by exactly this method. So there must be something else broken.
Sam
A: 

Did you installed the Nikon Raw Codec? Windows itself (and .NET Framework) does not know to handle Nikons Raw format.

Arthur
It does here on Vista. And I never installed any raw codec on this machine.
Joey
The code works on XP/Vista without the Nikon Raw Codec. On Windows 7 it does not work, regardless of any Raw Codecs.
Sam
A: 

.ImageFromFile has a but causing your app to connected to the file on HDD long after you are done with it.

You should read the image via a filestream so you don't stay linked to it. Even as far ahead as framework 3.5 MS has not fixed this bug

tlhintoq
I don't think this is related to my question at all, is it?
Sam
Btw, for your problem: did you dispose the image? I never had this problem even for tens of thousands of images, as long as I properly dispose them (I usually used using())
Sam
+1  A: 

I can confirm this behavior for JPEG photos coming from a Nikon Coolpix P5000.

It seems that there is some problem with the internal structure of the file, which confuses the GDI+ jpeg loader.I used an external image viewer just to recode the images as jpg and then everything went to normal. I cannot confirm if this behavior is found only on Win7

So did anybody found the cause of this? Or perhaps it's a bug in GDI+?

Andon