Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Taking a screen shot of a control

Taking a screen shot of a control

Scheduled Pinned Locked Moved C#
graphicshelpquestionlearning
12 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nike_arh
    wrote on last edited by
    #1

    Hi. I need to take a screen shot of a control - the control is plotterForm.expressionPlotter. I am using the following code: private Bitmap GetBitmap() { Bitmap bmp = new Bitmap(plotterForm.expressionPlotter.Width, plotterForm.expressionPlotter.Height); Graphics g = Graphics.FromImage(bmp); Screen screen = Screen.FromControl(plotterForm.expressionPlotter); g.CopyFromScreen(new Point(screen.Bounds.Location.X, screen.Bounds.Location.Y), Point.Empty, plotterForm.expressionPlotter.Size); return bmp; } The problem is that the location of which the screen shot is always the upper left corner of the desktop, not the upper left corner of the control. In other words the screen.Bounds.Location is a point which is always (0;0). When i added MessageBox.Show(screen.ToString()); i received the following message: Screen[Bounds={X=0,Y=0,Width=1024,Height=768} WorkingArea={X=0,Y=0,Width=1024,Height=739} Primary=True DeviceName=\\.\DISPLAY1 As if the screen hasn't captured the control. How can i fix this???

    Still learning...

    N A P R G 5 Replies Last reply
    0
    • N nike_arh

      Hi. I need to take a screen shot of a control - the control is plotterForm.expressionPlotter. I am using the following code: private Bitmap GetBitmap() { Bitmap bmp = new Bitmap(plotterForm.expressionPlotter.Width, plotterForm.expressionPlotter.Height); Graphics g = Graphics.FromImage(bmp); Screen screen = Screen.FromControl(plotterForm.expressionPlotter); g.CopyFromScreen(new Point(screen.Bounds.Location.X, screen.Bounds.Location.Y), Point.Empty, plotterForm.expressionPlotter.Size); return bmp; } The problem is that the location of which the screen shot is always the upper left corner of the desktop, not the upper left corner of the control. In other words the screen.Bounds.Location is a point which is always (0;0). When i added MessageBox.Show(screen.ToString()); i received the following message: Screen[Bounds={X=0,Y=0,Width=1024,Height=768} WorkingArea={X=0,Y=0,Width=1024,Height=739} Primary=True DeviceName=\\.\DISPLAY1 As if the screen hasn't captured the control. How can i fix this???

      Still learning...

      N Offline
      N Offline
      nike_arh
      wrote on last edited by
      #2

      I realized that the Screen.FromControl is something entirely different from the thing i expected! But here comes another question - how can i find the location of a control in relation to the desktop?

      Still learning...

      M 1 Reply Last reply
      0
      • N nike_arh

        I realized that the Screen.FromControl is something entirely different from the thing i expected! But here comes another question - how can i find the location of a control in relation to the desktop?

        Still learning...

        M Offline
        M Offline
        Matthew Butler 0
        wrote on last edited by
        #3

        Use... PointToScreen(control.Location);

        Matthew Butler

        1 Reply Last reply
        0
        • N nike_arh

          Hi. I need to take a screen shot of a control - the control is plotterForm.expressionPlotter. I am using the following code: private Bitmap GetBitmap() { Bitmap bmp = new Bitmap(plotterForm.expressionPlotter.Width, plotterForm.expressionPlotter.Height); Graphics g = Graphics.FromImage(bmp); Screen screen = Screen.FromControl(plotterForm.expressionPlotter); g.CopyFromScreen(new Point(screen.Bounds.Location.X, screen.Bounds.Location.Y), Point.Empty, plotterForm.expressionPlotter.Size); return bmp; } The problem is that the location of which the screen shot is always the upper left corner of the desktop, not the upper left corner of the control. In other words the screen.Bounds.Location is a point which is always (0;0). When i added MessageBox.Show(screen.ToString()); i received the following message: Screen[Bounds={X=0,Y=0,Width=1024,Height=768} WorkingArea={X=0,Y=0,Width=1024,Height=739} Primary=True DeviceName=\\.\DISPLAY1 As if the screen hasn't captured the control. How can i fix this???

          Still learning...

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          Can you not just use DrawToBitmap as in:

          int height = plotterForm.expressionPlotter.Height;
          int width = plotterForm.expressionPlotter.Width;
          Bitmap bmp = new Bitmap(width, height);
          plotterForm.expressionPlotter.DrawToBitmap(bmp, new Rectangle(0,0,width,height));

          Because if you could i imagine it will be much easier.

          My current favourite word is: Nipple!

          -SK Genius

          N 1 Reply Last reply
          0
          • N nike_arh

            Hi. I need to take a screen shot of a control - the control is plotterForm.expressionPlotter. I am using the following code: private Bitmap GetBitmap() { Bitmap bmp = new Bitmap(plotterForm.expressionPlotter.Width, plotterForm.expressionPlotter.Height); Graphics g = Graphics.FromImage(bmp); Screen screen = Screen.FromControl(plotterForm.expressionPlotter); g.CopyFromScreen(new Point(screen.Bounds.Location.X, screen.Bounds.Location.Y), Point.Empty, plotterForm.expressionPlotter.Size); return bmp; } The problem is that the location of which the screen shot is always the upper left corner of the desktop, not the upper left corner of the control. In other words the screen.Bounds.Location is a point which is always (0;0). When i added MessageBox.Show(screen.ToString()); i received the following message: Screen[Bounds={X=0,Y=0,Width=1024,Height=768} WorkingArea={X=0,Y=0,Width=1024,Height=739} Primary=True DeviceName=\\.\DISPLAY1 As if the screen hasn't captured the control. How can i fix this???

            Still learning...

            P Online
            P Online
            PIEBALDconsult
            wrote on last edited by
            #5

            Take a screen shot of the whole screen and then use an image editor? Or, what I did was write a simple applet that I can position over the section of the screen I want to capture and have do the capture.

            1 Reply Last reply
            0
            • A Anthony Mushrow

              Can you not just use DrawToBitmap as in:

              int height = plotterForm.expressionPlotter.Height;
              int width = plotterForm.expressionPlotter.Width;
              Bitmap bmp = new Bitmap(width, height);
              plotterForm.expressionPlotter.DrawToBitmap(bmp, new Rectangle(0,0,width,height));

              Because if you could i imagine it will be much easier.

              My current favourite word is: Nipple!

              -SK Genius

              N Offline
              N Offline
              nike_arh
              wrote on last edited by
              #6

              I changed the code of the project a little bit and the control which i want to capture is a panel. The panel has a written text using the DrawString method of the Graphics class.The screen shot is stored in the clipboard. When i take a screen shot using DrawToBitmap , when i paste the image in paint it appears, but the text isn't there. The taken screen shot[^] The image as it should be[^] Why doesn't the DrawToBitmap method capture the text???

              Still learning...

              A 1 Reply Last reply
              0
              • N nike_arh

                Hi. I need to take a screen shot of a control - the control is plotterForm.expressionPlotter. I am using the following code: private Bitmap GetBitmap() { Bitmap bmp = new Bitmap(plotterForm.expressionPlotter.Width, plotterForm.expressionPlotter.Height); Graphics g = Graphics.FromImage(bmp); Screen screen = Screen.FromControl(plotterForm.expressionPlotter); g.CopyFromScreen(new Point(screen.Bounds.Location.X, screen.Bounds.Location.Y), Point.Empty, plotterForm.expressionPlotter.Size); return bmp; } The problem is that the location of which the screen shot is always the upper left corner of the desktop, not the upper left corner of the control. In other words the screen.Bounds.Location is a point which is always (0;0). When i added MessageBox.Show(screen.ToString()); i received the following message: Screen[Bounds={X=0,Y=0,Width=1024,Height=768} WorkingArea={X=0,Y=0,Width=1024,Height=739} Primary=True DeviceName=\\.\DISPLAY1 As if the screen hasn't captured the control. How can i fix this???

                Still learning...

                R Offline
                R Offline
                Roger Alsing 0
                wrote on last edited by
                #7

                Im 99% sure that the DrawToBitmap method will invoke the OnPaint method on the control and pass a graphics object containing the bitmap. so if you draw the text outside of the OnPaint method, that code will not be triggered when calling DrawToBitmap. How are you drawing the text? Control.CreateGraphics?

                Blog: http://www.rogeralsing.com Projects: http://www.puzzleframework.com

                N 1 Reply Last reply
                0
                • R Roger Alsing 0

                  Im 99% sure that the DrawToBitmap method will invoke the OnPaint method on the control and pass a graphics object containing the bitmap. so if you draw the text outside of the OnPaint method, that code will not be triggered when calling DrawToBitmap. How are you drawing the text? Control.CreateGraphics?

                  Blog: http://www.rogeralsing.com Projects: http://www.puzzleframework.com

                  N Offline
                  N Offline
                  nike_arh
                  wrote on last edited by
                  #8

                  This is how things are: I have created a control - ExpressionPlotter. It is used to plot math graphs - they are drawn on the control. The control has a panel which contains the graphs' information. In the OnPaint method of the ExpressionPlotter is the method which draws the text - DisplayExpressionsText. In DisplayExpressionsText the panel is sized according to the text and then in the drawing of the text the method panel.CreateGraphics is used to obtain the Graphics class. When it comes to the screen shot the DrawToBitmap is called for the ExpressionPlotter control. I tried to draw the text in the Paint event of the panel, but when i started the program, the panel didn't show.

                  Still learning...

                  N G 2 Replies Last reply
                  0
                  • N nike_arh

                    I changed the code of the project a little bit and the control which i want to capture is a panel. The panel has a written text using the DrawString method of the Graphics class.The screen shot is stored in the clipboard. When i take a screen shot using DrawToBitmap , when i paste the image in paint it appears, but the text isn't there. The taken screen shot[^] The image as it should be[^] Why doesn't the DrawToBitmap method capture the text???

                    Still learning...

                    A Offline
                    A Offline
                    Anthony Mushrow
                    wrote on last edited by
                    #9

                    i don't know, i had trouble with a textbox not drawing the text when i used DrawToBitmap. Then all of a sudden it just started working. and i'm not sure why/

                    My current favourite word is: Nipple!

                    -SK Genius

                    1 Reply Last reply
                    0
                    • N nike_arh

                      This is how things are: I have created a control - ExpressionPlotter. It is used to plot math graphs - they are drawn on the control. The control has a panel which contains the graphs' information. In the OnPaint method of the ExpressionPlotter is the method which draws the text - DisplayExpressionsText. In DisplayExpressionsText the panel is sized according to the text and then in the drawing of the text the method panel.CreateGraphics is used to obtain the Graphics class. When it comes to the screen shot the DrawToBitmap is called for the ExpressionPlotter control. I tried to draw the text in the Paint event of the panel, but when i started the program, the panel didn't show.

                      Still learning...

                      N Offline
                      N Offline
                      nike_arh
                      wrote on last edited by
                      #10

                      I did it! I moved the resizing of the panel to the OnPaint. Then i moved the drawing of the text to the Paint event of the panel. When i want to draw the text now i only have to Refresh() the panel. :rolleyes:

                      Still learning...

                      1 Reply Last reply
                      0
                      • N nike_arh

                        Hi. I need to take a screen shot of a control - the control is plotterForm.expressionPlotter. I am using the following code: private Bitmap GetBitmap() { Bitmap bmp = new Bitmap(plotterForm.expressionPlotter.Width, plotterForm.expressionPlotter.Height); Graphics g = Graphics.FromImage(bmp); Screen screen = Screen.FromControl(plotterForm.expressionPlotter); g.CopyFromScreen(new Point(screen.Bounds.Location.X, screen.Bounds.Location.Y), Point.Empty, plotterForm.expressionPlotter.Size); return bmp; } The problem is that the location of which the screen shot is always the upper left corner of the desktop, not the upper left corner of the control. In other words the screen.Bounds.Location is a point which is always (0;0). When i added MessageBox.Show(screen.ToString()); i received the following message: Screen[Bounds={X=0,Y=0,Width=1024,Height=768} WorkingArea={X=0,Y=0,Width=1024,Height=739} Primary=True DeviceName=\\.\DISPLAY1 As if the screen hasn't captured the control. How can i fix this???

                        Still learning...

                        G Offline
                        G Offline
                        GuyThiebaut
                        wrote on last edited by
                        #11

                        Take a look at my article clickety in which I cover basic screen capture as well as capturing a specific area of the screen. The source code is available for download from the article as well. Regards Guy

                        You always pass failure on the way to success.
                        1 Reply Last reply
                        0
                        • N nike_arh

                          This is how things are: I have created a control - ExpressionPlotter. It is used to plot math graphs - they are drawn on the control. The control has a panel which contains the graphs' information. In the OnPaint method of the ExpressionPlotter is the method which draws the text - DisplayExpressionsText. In DisplayExpressionsText the panel is sized according to the text and then in the drawing of the text the method panel.CreateGraphics is used to obtain the Graphics class. When it comes to the screen shot the DrawToBitmap is called for the ExpressionPlotter control. I tried to draw the text in the Paint event of the panel, but when i started the program, the panel didn't show.

                          Still learning...

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #12

                          nike_arh wrote:

                          in the drawing of the text the method panel.CreateGraphics is used to obtain the Graphics class.

                          That's your problem. That means that you are always drawing the text on the screen. Use the Graphics object that is supplied in the event arguments to the Paint event.

                          Despite everything, the person most likely to be fooling you next is yourself.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups