System.Drawing.dll中的StackOverflowException错误 [英] StackOverflowException accurs in System.Drawing.dll

查看:106
本文介绍了System.Drawing.dll中的StackOverflowException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个透明标签.我将标签拖动到窗体.我将大量(例如Size(200,200))设置为标签的大小并运行了表单,因此引发了异常.
谁能告诉我如何纠正它?

我的海关标签代码:

I made a transparent label. I dragged the label to a Form. I set a large number(e.g. Size(200,200)) to the size of the label and ran the form, an exception was raised.
Who can tell me how to make it to correct?

My Customs Label code:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace MyNamSpace
{
    public partial class TransparentLabel : System.Windows.Forms.Label
    {
        private void UpdateRegion()
        {
            Bitmap foregroundBitmap = new Bitmap(this.Width, this.Height);

            using (Graphics g = Graphics.FromImage(foregroundBitmap))
            {
                this.DrawForeground(g);
            }

            int w = foregroundBitmap.Width;
            int h = foregroundBitmap.Height;
            Rectangle rect = new Rectangle(0, 0, w, h);
            Region region = new Region(rect);

            BitmapData bd = foregroundBitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int stride = bd.Stride;
            int bytes = stride * h;
            byte[] bgraValues = new byte[bytes];
            Marshal.Copy(bd.Scan0, bgraValues, 0, bytes);
            foregroundBitmap.UnlockBits(bd);
            foregroundBitmap.Dispose();

            int line;
            for (int y = 0; y < h; y++)
            {
                line = stride * y;
                for (int x = 0; x < w; x++)
                {
                    if (bgraValues[line + x * 4 + 3] == 0)
                    {
                        region.Exclude(new Rectangle(x, y, 1, 1));
                    }
                }
            }
            this.Region = region;
        }

        private void DrawForeground(Graphics g)
        {
            using (SolidBrush sb = new SolidBrush(this.ForeColor))
            {
                Rectangle r = new Rectangle(
                    this.Padding.Left,
                    this.Padding.Top,
                    this.ClientRectangle.Width - this.Padding.Left - this.Padding.Right,
                    this.ClientRectangle.Height - this.Padding.Top - this.Padding.Bottom
                );

                g.DrawString(this.Text, this.Font, sb, r);
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            if ((this.DesignMode == false) && (this.BackColor == Color.Transparent))
            {
                this.UpdateRegion();
            }
            base.OnTextChanged(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            if ((this.DesignMode == false) && (this.BackColor == Color.Transparent))
            {
                this.UpdateRegion();
            }
            base.OnSizeChanged(e);
        }

        protected override void OnPaddingChanged(EventArgs e)
        {
            if ((this.DesignMode == false) && (this.BackColor == Color.Transparent))
            {
                this.UpdateRegion();
            }
            base.OnPaddingChanged(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.BackColor != Color.Transparent)
            {
                base.OnPaint(e);
            }
            else
            {
                this.DrawForeground(e.Graphics);
            }
        }
    }
}

推荐答案

可能发生的事情是导致您的SizeChanged事件处理程序触发.这会调用UpdateRegion并触发SizeChanged,依此类推,以此类推.........

或类似的东西.

在开始尝试解决方案之前,您将需要检查堆栈跟踪以确定有问题的方法.

或者要么使用旧的一次注释事件处理程序"方法,直到找到罪魁祸首.
What could be happening is that something causes your SizeChanged event handler to fire. This calls UpdateRegion which triggers SizeChanged and so on and so on..................

Or something similar to that.

You will need to examine a Stack Trace to determine the offending Methods before you can start to attempt a solution.

Either that or use the old ''comment out the event handlers one at a time'' methodology till you find the culprit.


你好,亨利·分钟(Henry Minute)
非常感谢.
hello Henry Minute
thank you very much.


大家好

问题已得到纠正.

更改的代码:

GraphicsPath gp =新的GraphicsPath(FillMode.Alternate);
if(bgraValues [line + x * 4 + 3]!= 0)
{
gp.AddRectangle(new Rectangle(x,y,1,1));
}
this.Region = new Region(gp);

非常感谢
hello everyone

the problem was made to correctted.

changed code:

GraphicsPath gp = new GraphicsPath(FillMode.Alternate);
if (bgraValues[line + x * 4 + 3] != 0)
{
gp.AddRectangle(new Rectangle(x, y, 1, 1));
}
this.Region = new Region(gp);

thanks a lot


这篇关于System.Drawing.dll中的StackOverflowException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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