我正在尝试使用winform面板中的Graphics制作加载动画。加载器的角度在后台工作程序中更改,然后进入休眠状态,然后再次更改。
这是LoadingAnimation类代码:
class LoadingAnimation : Panel
{
int _angle = 0;
float _penSize = 12f;
[Category("Appearance")]
[Description("Gets or sets the angle.")]
[Browsable(true)]
public int Angle
{
get
{
return _angle;
}
set
{
_angle = value;
this.Invalidate();
}
}
[Category("Appearance")]
[Description("Gets or sets the pen size.")]
[Browsable(true)]
public float PenSize
{
get
{
return _penSize;
}
set
{
_penSize = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
//e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (Pen p = new Pen(Color.FromArgb(0, 162, 255), _penSize))
{
e.Graphics.DrawEllipse(p, (_penSize / 2) + 1, (_penSize / 2) + 1, this.ClientRectangle.Width - (_penSize + 2), this.ClientRectangle.Height - (_penSize + 2));
p.Color = Color.White;
e.Graphics.DrawArc(p, (_penSize / 2) + 1, (_penSize / 2) + 1, this.ClientRectangle.Width - (_penSize + 2), this.ClientRectangle.Height - (_penSize + 2), _angle, 90);
}
}
}
这是更新角度的后台工作人员dowork事件。
void Animator_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
try
{
if (angle > 359)
{
angle = 0;
}
if (angle >= 0 && angle <= 90)
{
time = 8;
}
if (angle > 90 && angle <= 180)
{
time = 15;
}
if (angle > 180 && angle <= 270)
{
time = 20;
}
if (angle > 170 && angle <= 359)
{
time = 2;
}
picStatus.Angle = angle;
angle++;
Thread.Sleep(time);
}
catch { }
}
}
它的工作正常,直到表单大小不变。当我最小化表单然后将表单返回到原始状态时,它会抛出错误
System.Drawing.dll中出现未处理的“System.ArgumentException”类型异常
附加信息:参数无效。
它没有显示或跳转到发生异常的任何行,只是指向主winform构造函数调用。
以下是截图:
更新:
我尝试使用图像并旋转它。对于图像旋转,我引用并使用了本文中的代码。 C# Image/PictureBox Rotations
并使用此代码进行图片旋转
void ImageRotator_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
try
{
if (angle > 359)
{
angle = 0;
}
if (angle >= 0 && angle <= 90)
{
time = 8;
}
if (angle > 90 && angle <= 180)
{
time = 15;
}
if (angle > 180 && angle <= 270)
{
time = 20;
}
if (angle > 170 && angle <= 359)
{
time = 2;
}
//Store our old image so we can delete it
Image oldImage = picStatus.BackgroundImage;
//Pass in our original image and return a new image rotated 35 degrees right
picStatus.BackgroundImage = Utilities.RotateImage(Resources.Loader, angle);
if (oldImage != null)
{
oldImage.Dispose();
}
angle++;
Thread.Sleep(time);
}
catch { }
}
}
但问题仍然存在。与屏幕截图中给出的错误相同。