开发者

C# Parameter is Not Valid Error

开发者 https://www.devze.com 2022-12-10 20:38 出处:网络
I keep getting an error in the following code when I close the print preview window or move the print preview window.I can\'t seem to understand why this is happening.It happens on the g.DrawString()

I keep getting an error in the following code when I close the print preview window or move the print preview window. I can't seem to understand why this is happening. It happens on the g.DrawString() line. As far as I can tell nothing has been disposed of either.

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;
        }
    }

First part of the error:

   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)

Any help would be appreciated.

Thanks.


I encountered the same error not long ago. The reason was that one of the objects was already disposed...

Maybe the font is being disposed somewhere else, or the graphics object itself. I don't think the brush would cause problems though because it's local to the method and we see that it's not disposed.

Edit:

To know whether or not a graphics object is disposed is easy: all of it's properties throw an exception. It's not as easy for a font though, because all properties still work. One way that I found to check whether the font is disposed or not is to try to clone it (you can add font.Clone() in the Watch window to test it). If the clone works, the the font is not disposed. Otherwise it will throw an exception.


Do you need to explicitly have your x/y coordinates be float (i.e. 0.0f instead of 0)? I would expect a compile error from that, not a runtime error so probably not.


I haven't had done that much with OnPaint... Everything you show is about Rectangles. Are you rotating a rectangle or a string? If it is a rectangle shouldn't it be .DrawRectangle instead of .DrawString?


In case anybody has the same error, I found out that performing the transformations in "separate steps" solves the problem.

using (var graphics = Graphics.FromImage(destImage))
            {
                using (var wrapMode = new ImageAttributes())
                {


                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.CompositingMode = CompositingMode.SourceCopy;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode = SmoothingMode.HighQuality;
                    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);

                }              
            }

            using (var graphics = Graphics.FromImage(destImage))
            {
                var font = new Font(new FontFamily("Arial"), 16, FontStyle.Regular, GraphicsUnit.Pixel);
                var brush = new SolidBrush(Color.White);
                graphics.DrawString("text to add", font, brush, 10F, 10F);
                font.Dispose();
                brush.Dispose();
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消