位图构造函数在Win XP中抛出ArgumentException而不是Win 7 - 为什么? [英] Bitmap Constructor Throws ArgumentException in Win XP but not Win 7 - why?

查看:74
本文介绍了位图构造函数在Win XP中抛出ArgumentException而不是Win 7 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在.NET中遇到Bitmap构造问题 - 它在Windows 7中工作正常,但在XP中抛出ArgumentException,这是根据MSDN文档加倍混淆,Bitmap构造函数抛出的唯一例外是FileNotFoundException。



作为我正在进行的项目的一部分,有一个类哪个是Button类的扩展。在这个内部有BackgroundImageName属性,它取一个图像文件的名称(在一个已知的位置),然后将按钮的背景图像设置为此(有理由以这种方式工作)。



此属性的设置代码如下:



  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 位图(ImagePath);
}
}
}





我已经隔离了代码和抛出异常的行是100%绝对

  base  .BackgroundImage =  new 位图(ImagePath); 

(不是Path.Combine调用)。



文件肯定存在,而且它们也很小,所以应该没有内存问题。 _ImagePath是一个已知的文件夹位置。



就像我说的,这段代码在Windows 7上完美运行,但在Windows XP中完全没有Argument异常。



这个怪异的原因在于,当首次在XP上加载对象时,它才能正常工作。只有当您以编程方式尝试调用此setter时才会失败。一旦它倒下,它就会在程序的各处落下。



我很遗憾,对Bitmap类的任何建议或更详细的了解都将不胜感激 - 谢谢。

解决方案

唯一可能的例外是FileNotFound,这显然是错误的。该文件当然可以存在,但对它有一个独占锁 - 在这种情况下构造函数必须失败,但不是因为找不到文件。



文档说:



文件保持锁定状态,直到处理掉位图。



它是很可能在XP上,文件锁定的类型与Win 7不同。



XP上打开的文件可能有独占锁定,而Win 7上的那个有一个共享读锁。



如果是这种情况,那么在文件静止的时候尝试再次创建位图锁定可能会在XP上引发异常。



由于您无法控制何时实际处理Bitmap,您可能需要重新编写代码确保你只创建一次位图。



也许保留你创建的位图的全局缓存 - 按文件名索引。然后,如果您再次需要相同的位图,只需使用已有的位图。



或者使用Bitmap(image)构造函数从您使用该文件创建的Bitmap中创建一个新的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.

解决方案

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.


这篇关于位图构造函数在Win XP中抛出ArgumentException而不是Win 7 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆