Ask a questionAsk a question
 

AnswerImage of control

  • Tuesday, November 03, 2009 7:12 PMhiggernaut Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is there any way to save an image of a control?

    I have a graph that I am using from a package, but I made a custom control that would hover with your mouse and tell you the Y values according to the mouse's position relative to X on this pre-made graph control. If you click on the control, it will "paste" my control onto the graph. The graph can be exported as a .bmp, but since my control is not part of the graph, it does not get exported.


    Thanks.
    • Edited byhiggernaut Wednesday, November 04, 2009 9:48 PM
    •  

Answers

  • Tuesday, November 03, 2009 7:31 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    Hi,

    You can use the following method.

            public Image GetImageOfControl(Control c)
            {
                Rectangle rc = new Rectangle(this.PointToScreen(c.Location), c.Size);
                Image i = new Bitmap(rc.Width, rc.Height);
                Graphics g = Graphics.FromImage(i);
                g.CopyFromScreen(rc.Left, rc.Top, 0, 0, rc.Size);
                g.Dispose();
                return i;
            }
    
    • Marked As Answer byhiggernaut Tuesday, November 03, 2009 7:33 PM
    •  

All Replies

  • Tuesday, November 03, 2009 7:31 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    Hi,

    You can use the following method.

            public Image GetImageOfControl(Control c)
            {
                Rectangle rc = new Rectangle(this.PointToScreen(c.Location), c.Size);
                Image i = new Bitmap(rc.Width, rc.Height);
                Graphics g = Graphics.FromImage(i);
                g.CopyFromScreen(rc.Left, rc.Top, 0, 0, rc.Size);
                g.Dispose();
                return i;
            }
    
    • Marked As Answer byhiggernaut Tuesday, November 03, 2009 7:33 PM
    •  
  • Wednesday, November 04, 2009 9:47 PMhiggernaut Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    That did the trick! Just needed the tiniest bit of retooling for C++.


    Thanks!