代码之家  ›  专栏  ›  技术社区  ›  Nemo

从面板截图

  •  -1
  • Nemo  · 技术社区  · 8 年前

    我试图从一个面板上截图,我在上面画了一些水平线。 Panel Drawn

    我使用了以下代码:

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        //hide close button
        pictureBox1.Visible = false;
    
        //Take screenshoot for the DPanel.
        Bitmap dend = new Bitmap(dPanel.Width, dPanel.Height);
        Graphics G = Graphics.FromImage(dend);
        System.Drawing.Rectangle rectangle = dPanel.RectangleToScreen(dPanel.ClientRectangle);
        G.CopyFromScreen(rectangle.Location, System.Drawing.Point.Empty, dPanel.Size);
        SomeGlobalVariables.Mygraph = dend;
    
        Reporttoprint r = new Reporttoprint();
        r.Show();
    }
    

    单击linkLabel1时发生异常。

    Exception

    似乎当我单击linkLabel1执行代码并从面板中拍摄时,会再次调用dPanel\u Paint action,然后引发异常!

    此区域的例外情况:

    private void dPanel_Paint(object sender, PaintEventArgs e)
    {
        if (k==0)
        {
            //add the "Group" center point to the dictionary.
            ClusterPoint=new Point(((ep1.X+ep2.X)/2),((ep1.Y+ep2.Y)/2));
            //Exception is done here
            clusterpoints.Add(Clusterchar[k], ClusterPoint);
        }
    }
    

    任何帮助
    提前谢谢。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Zer0    8 年前

    首先,绘制事件非常频繁地激发(取决于)。

    问题是您有重复的密钥。两种解决方案。

    覆盖现有密钥/值对:

    clusterpoints[Clusterchar[k]] = ClusterPoint;
    

    或者,如果密钥已存在,则根本不添加:

    var key = Clusterchar[k];
    if(!clusterpoints.ContainsKey(key))
        clusterpoints.Add(key, ClusterPoint);