Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET4 Bitmap
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:
 
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
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 13-Feb-13 5:19am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Martin P. Davies at 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 at 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
0 OriginalGriff 470
1 Maciej Los 345
2 Richard MacCutchan 230
3 Mathew Soji 220
4 BillWoodruff 180
0 OriginalGriff 8,834
1 Sergey Alexandrovich Kryukov 7,477
2 DamithSL 5,689
3 Maciej Los 5,329
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100