Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

I have a problem with the Bitmap constuctor in .NET - it works fine in Windows 7, but throws an ArgumentException in XP, which is doubly confusing as according to MSDN documentation, the only exception thrown by the Bitmap constructor is a FileNotFoundException.

As part of a project I am working on, there is a class which an extension of the Button class. Within this there is BackgroundImageName property, which takes the name of an image file (in a known location) and then sets the background image of the button to this (there are reasons for it working this way).

The Setting code for this property is below:

C#
public String BackgroundImageName {
    get { return _BackgroundImage; }
    set {
        _BackgroundImage = value;

        if (String.IsNullOrEmpty(_BackgroundImage)) {
            base.BackgroundImage = null;
        }
        else {
            String ImagePath = Path.Combine(_ImagePath, _BackgroundImage);
            base.BackgroundImage = new Bitmap(ImagePath);
        }
    }
}


I have isolated the code and the line that throws the exception is 100% definitely
C#
base.BackgroundImage = new Bitmap(ImagePath);
(not the Path.Combine call).

The files definitely exist, and they are also small so there should be no memory issues. _ImagePath is a known folder location.

Like I said, this code works perfectly on Windows 7, but falls over completely in Windows XP with the Argument exception.

What makes this weirder is that when the objects are first loaded on XP, it DOES work. Only when you programmatically try to call this setter does it fall down. And once it has fallen down, it then falls down everywhere in the program.

I am at a loss and any suggestions or more detailed knowledge of the Bitmap class would be greatly appreciated - thank you.
Posted

1 solution

It's patently false that the only possible exception is FileNotFound. The file could certainly existed but have an exclusive lock on it -- in which case the constructor would have to fail, but not because the file wasn't found.

Documentation says:

"The file remains locked until the Bitmap is disposed."

It is quite possible that on XP the type of "file lock" is different than on Win 7.

It's likely that the file opened on XP has an exclusive lock, while the one on Win 7 has a shared-read lock.

If that's the case, then trying to create the bitmap again while the file is still locked could raise an exception on XP.

Since you don't have any control of when the Bitmap is actually disposed, you might have to re-write you code to ensure you only ever create the bitmap once.

Maybe keep a global cache of the bitmaps you've created -- indexed by filename. Then if you need the same bitmap again just use the already existing one.

Or use the Bitmap(image) constructor to make a new Bitmap from the one you created with the file, and then immediately dispose of the one you created with the file.
 
Share this answer
 
Comments
Martin P. Davies 14-Feb-13 4:51am    
Thanks for the response - I agree, I can't see how FileNotFound is the only exception that can be thrown.

I have to admit I was thinking along the same lines regarding locking - thank you for all the suggestions - I'll have a play and see if I can isolate it further, but yes, I agree I think some difference in the locking mechanism is occurring on each platform.

Thank you again - much appreciated.
TRK3 14-Feb-13 13:28pm    
If I remember correctly, I ran into a very similar problem a year or two ago, with C++/MFC's CBitmap.

I was using a third party library of UI-widgets (which I fortunately had source for). After some experimentation it boiled down to the fact that I couldn't create two CBitmaps from the same image file at the same time, but I could make a copy of an existing CBitmap.

I don't recall if it was an XP specific problem, but at the time I was developing on Vista and testing on XP and Vista, so it's probably exactly the same as what you are running into. (Pretty sure the .NET Bitmap is just a wrappper around the same Win32 object that CBitmap wraps.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900