当我关闭打印预览窗口或移动打印预览窗口时,我在以下代码中出现错误.我似乎无法理解为什么会这样.它发生在g.DrawString()行上.据我所知,没有任何事情被处理掉.
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush textBrush = new SolidBrush(this.ForeColor);
float width = TextRenderer.MeasureText(Text, this.Font).Width;
float height = TextRenderer.MeasureText(Text, this.Font).Height;
float radius = 0f;
if (ClientRectangle.Width < ClientRectangle.Height)
radius = ClientRectangle.Width * 0.9f / 2;
else
radius = ClientRectangle.Height * 0.9f / 2;
switch (orientation)
{
case Orientation.Rotate:
{
double angle = (_rotationAngle / 180) * Math.PI;
g.TranslateTransform(
(ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle))) / 2,
(ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle))) / 2);
g.RotateTransform((float)_rotationAngle);
g.DrawString(Text, this.Font, textBrush, 0, 0);
g.ResetTransform();
}
break;
}
}
Run Code Online (Sandbox Code Playgroud)
错误的第一部分:
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y)
at ScanPro.CustomControls.UserLabel.OnPaint(PaintEventArgs e)
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
谢谢.
Met*_*ght 21
我不久前遇到了同样的错误.原因是其中一个物品已被处理掉......
也许字体被放置在其他地方,或图形对象本身.我不认为刷子会引起问题,因为它是方法的本地,我们发现它没有被处理掉.
编辑:
要知道图形对象是否处理起来很容易:它的所有属性都会抛出异常.但是对于字体来说并不容易,因为所有属性仍然有用.我发现检查字体是否被处理的一种方法是尝试克隆它(您可以在Watch窗口中添加font.Clone()来测试它).如果克隆工作,则不处理该字体.否则会抛出异常.