SolidBrush.Clone 抛出异常
SolidBrush.Clone throws exception
我正在使用不同类型的笔刷,例如 SolidBrush
、HatchBrush
等...,在我的 windows 表单控件中绘制复杂的形状和文本。我使用的代码是,
protected void FillShape(Brush myBrush, GraphicsPath shape)
{
if (myBrush is SolidBrush)
{
using (SolidBrush sbr = myBrush.Clone() as SolidBrush)
{
//Code for drawing with solid brush
}
}
else if (myBrush is HatchBrush)
{
//Code for drawing with hatch brush
}
else if (myBrush is LinearGradientBrush)
{
//Code for drawing with linear gradient brush
}
else if (myBrush is PathGradientBrush)
{
//Code for drawing with path gradient brush
}
}
有时,行 myBrush.Clone()
会抛出 ArgumentException
。我在 MSDN link 中搜索了 SolidBrush.Clone() 方法,但没有关于任何异常的信息
SolidBrush.Clone
所以,我在下面linkReference code for SolidBrush class
检查了这个方法的源代码
System.Drawing.SolidBrush.Clone
方法中使用的代码是
public override object Clone()
{
IntPtr cloneBrush = IntPtr.Zero;
int status = SafeNativeMethods.Gdip.GdipCloneBrush(new HandleRef(this, this.NativeBrush), out cloneBrush);
if (status != SafeNativeMethods.Gdip.Ok)
throw SafeNativeMethods.Gdip.StatusException(status);
// We intentionally lose the "immutable" bit.
return new SolidBrush(cloneBrush);
}
很明显,Clone
方法可能会抛出异常,但我无法确定它何时或为何会抛出异常,因为这段代码不是我的。
有谁知道此方法何时或为何会抛出异常?
编辑:
附上显示异常详细信息的图片
另外,这不是系统刷机,因为刷机的immutable
属性是false
您可以确定它实际上是 Clone()
抛出异常还是处置。如果您尝试处置或更改 SystemBrush,则会发生异常:
SystemBrushes.Control.Dispose(); // ArgumentException: Changes cannot be made to Brush because permissions are not valid.
但是克隆它然后处理它工作正常:
((Brush) SystemBrushes.Control.Clone()).Dispose(); // OK
所以,如果您认为这是问题所在,也许完全避免 Clone
,例如:
// instead of using clone, just make a new instance
Brush myBrush = SystemBrushes.Control;
if (myBrush is SolidBrush) {
using (var nb = new SolidBrush(((SolidBrush) myBrush).Color)) {
// ...
}
}
我正在使用不同类型的笔刷,例如 SolidBrush
、HatchBrush
等...,在我的 windows 表单控件中绘制复杂的形状和文本。我使用的代码是,
protected void FillShape(Brush myBrush, GraphicsPath shape)
{
if (myBrush is SolidBrush)
{
using (SolidBrush sbr = myBrush.Clone() as SolidBrush)
{
//Code for drawing with solid brush
}
}
else if (myBrush is HatchBrush)
{
//Code for drawing with hatch brush
}
else if (myBrush is LinearGradientBrush)
{
//Code for drawing with linear gradient brush
}
else if (myBrush is PathGradientBrush)
{
//Code for drawing with path gradient brush
}
}
有时,行 myBrush.Clone()
会抛出 ArgumentException
。我在 MSDN link 中搜索了 SolidBrush.Clone() 方法,但没有关于任何异常的信息
SolidBrush.Clone
所以,我在下面linkReference code for SolidBrush class
检查了这个方法的源代码System.Drawing.SolidBrush.Clone
方法中使用的代码是
public override object Clone()
{
IntPtr cloneBrush = IntPtr.Zero;
int status = SafeNativeMethods.Gdip.GdipCloneBrush(new HandleRef(this, this.NativeBrush), out cloneBrush);
if (status != SafeNativeMethods.Gdip.Ok)
throw SafeNativeMethods.Gdip.StatusException(status);
// We intentionally lose the "immutable" bit.
return new SolidBrush(cloneBrush);
}
很明显,Clone
方法可能会抛出异常,但我无法确定它何时或为何会抛出异常,因为这段代码不是我的。
有谁知道此方法何时或为何会抛出异常?
编辑:
附上显示异常详细信息的图片
另外,这不是系统刷机,因为刷机的immutable
属性是false
您可以确定它实际上是 Clone()
抛出异常还是处置。如果您尝试处置或更改 SystemBrush,则会发生异常:
SystemBrushes.Control.Dispose(); // ArgumentException: Changes cannot be made to Brush because permissions are not valid.
但是克隆它然后处理它工作正常:
((Brush) SystemBrushes.Control.Clone()).Dispose(); // OK
所以,如果您认为这是问题所在,也许完全避免 Clone
,例如:
// instead of using clone, just make a new instance
Brush myBrush = SystemBrushes.Control;
if (myBrush is SolidBrush) {
using (var nb = new SolidBrush(((SolidBrush) myBrush).Color)) {
// ...
}
}