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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Does anyone know how to capture screen in C#?

Does anyone know how to capture screen in C#?

Scheduled Pinned Locked Moved C#
csharpgraphicstutorialquestion
11 Posts 3 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.
  • L Offline
    L Offline
    Li kai Liu Angus
    wrote on last edited by
    #1

    Hi, I'm thinking of capturing some regions of my screen into a Bitmap object. Does anyone have any ideas? Thanks:) Li-kai Liu

    L P 2 Replies Last reply
    0
    • L Li kai Liu Angus

      Hi, I'm thinking of capturing some regions of my screen into a Bitmap object. Does anyone have any ideas? Thanks:) Li-kai Liu

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Hi Eric Gunnerson has a library available on GotDotNet.com[^]. The zip is called Win32window.zip, was a bit tricky to find. You can email me if you cant find it. Unfortunately its a bit long too paste :( Cheers Give them a chance! Do it for the kittens, dear God, the kittens!

      1 Reply Last reply
      0
      • L Li kai Liu Angus

        Hi, I'm thinking of capturing some regions of my screen into a Bitmap object. Does anyone have any ideas? Thanks:) Li-kai Liu

        P Offline
        P Offline
        Philip Fitzsimons
        wrote on last edited by
        #3

        try this... public Bitmap CaptureWindow(string windowTitle) { RECT rect = new RECT(); IntPtr windowDC; IntPtr dc1; IntPtr hWnd; // get the window device context for drawing if (windowTitle == null) { hWnd = FindWindow(null, "DISPLAY"); // get Win32 handle - must be free'ed later windowDC = GetWindowDC(hWnd); // window is size of screen rect.right = Screen.PrimaryScreen.Bounds.Width; rect.bottom = Screen.PrimaryScreen.Bounds.Height; } else { hWnd = FindWindow(null, windowTitle); // get Win32 handle - must be free'ed later windowDC = GetWindowDC(hWnd); // work out size of window int returnCode = GetWindowRgnBox(hWnd, ref rect); } // create a bitmap to hold the picture Graphics g1 = Graphics.FromHdc(windowDC); Bitmap MyImage = new Bitmap(rect.right, rect.bottom, g1); Graphics g2 = Graphics.FromImage(MyImage); // get the device contexts dc1 = g1.GetHdc(); IntPtr dc2 = g2.GetHdc(); // ask Win32 to copy the bitmap on the screen to our bitmap BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc1, 0, 0, 13369376); // give back the handles g1.ReleaseHdc(dc1); g2.ReleaseHdc(dc2); // free up Win32 handle ReleaseDC(hWnd, windowDC); return MyImage; } [DllImportAttribute("gdi32.dll")] private static extern bool BitBlt( IntPtr hdcDest, // handle to destination DC int nXDest, // x-coord of destination upper-left corner int nYDest, // y-coord of destination upper-left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle IntPtr hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y-coordinate of source upper-left corner System.Int32 dwRop // raster operation code ); [DllImportAttribute("gdi32.dll")] private static extern IntPtr CreateDC( string lpszDriver, // driver name string lpszDevice, // device name string lpszOutput, // not used; should be NULL IntPtr lpInitData // optional printer data ); [DllImportAttribute("User32.dll")] private static extern IntPtr FindWindow( string lpClassName, // class name string lpWindowName // window name ); [DllImportAttribute("User32.dll")] private static extern IntPtr GetWindowDC( IntPtr hWnd // handle to window );

        L L 3 Replies Last reply
        0
        • P Philip Fitzsimons

          try this... public Bitmap CaptureWindow(string windowTitle) { RECT rect = new RECT(); IntPtr windowDC; IntPtr dc1; IntPtr hWnd; // get the window device context for drawing if (windowTitle == null) { hWnd = FindWindow(null, "DISPLAY"); // get Win32 handle - must be free'ed later windowDC = GetWindowDC(hWnd); // window is size of screen rect.right = Screen.PrimaryScreen.Bounds.Width; rect.bottom = Screen.PrimaryScreen.Bounds.Height; } else { hWnd = FindWindow(null, windowTitle); // get Win32 handle - must be free'ed later windowDC = GetWindowDC(hWnd); // work out size of window int returnCode = GetWindowRgnBox(hWnd, ref rect); } // create a bitmap to hold the picture Graphics g1 = Graphics.FromHdc(windowDC); Bitmap MyImage = new Bitmap(rect.right, rect.bottom, g1); Graphics g2 = Graphics.FromImage(MyImage); // get the device contexts dc1 = g1.GetHdc(); IntPtr dc2 = g2.GetHdc(); // ask Win32 to copy the bitmap on the screen to our bitmap BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc1, 0, 0, 13369376); // give back the handles g1.ReleaseHdc(dc1); g2.ReleaseHdc(dc2); // free up Win32 handle ReleaseDC(hWnd, windowDC); return MyImage; } [DllImportAttribute("gdi32.dll")] private static extern bool BitBlt( IntPtr hdcDest, // handle to destination DC int nXDest, // x-coord of destination upper-left corner int nYDest, // y-coord of destination upper-left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle IntPtr hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y-coordinate of source upper-left corner System.Int32 dwRop // raster operation code ); [DllImportAttribute("gdi32.dll")] private static extern IntPtr CreateDC( string lpszDriver, // driver name string lpszDevice, // device name string lpszOutput, // not used; should be NULL IntPtr lpInitData // optional printer data ); [DllImportAttribute("User32.dll")] private static extern IntPtr FindWindow( string lpClassName, // class name string lpWindowName // window name ); [DllImportAttribute("User32.dll")] private static extern IntPtr GetWindowDC( IntPtr hWnd // handle to window );

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Philip Fitzsimons wrote: [StructLayout(LayoutKind.Sequential)] struct RECT { public Int32 left; public Int32 top; public Int32 right; public Int32 bottom; } OK, tell me, why dont you use the Rectangle struct defined in the .NET framework? Give them a chance! Do it for the kittens, dear God, the kittens!

          P 1 Reply Last reply
          0
          • L leppie

            Philip Fitzsimons wrote: [StructLayout(LayoutKind.Sequential)] struct RECT { public Int32 left; public Int32 top; public Int32 right; public Int32 bottom; } OK, tell me, why dont you use the Rectangle struct defined in the .NET framework? Give them a chance! Do it for the kittens, dear God, the kittens!

            P Offline
            P Offline
            Philip Fitzsimons
            wrote on last edited by
            #5

            1. 'cause it did not occur to me. 2. 'cause I just translated the API directly, I was not trying to be clever :) good point.


            "When the only tool you have is a hammer, a sore thumb you will have."

            L 1 Reply Last reply
            0
            • P Philip Fitzsimons

              1. 'cause it did not occur to me. 2. 'cause I just translated the API directly, I was not trying to be clever :) good point.


              "When the only tool you have is a hammer, a sore thumb you will have."

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              No, I was just wondering. :) In my experience Rectangle and the other Drawing structs have been fine for marshalling. I have seen people this alot though...even MS people :laugh: Give them a chance! Do it for the kittens, dear God, the kittens!

              L 1 Reply Last reply
              0
              • P Philip Fitzsimons

                try this... public Bitmap CaptureWindow(string windowTitle) { RECT rect = new RECT(); IntPtr windowDC; IntPtr dc1; IntPtr hWnd; // get the window device context for drawing if (windowTitle == null) { hWnd = FindWindow(null, "DISPLAY"); // get Win32 handle - must be free'ed later windowDC = GetWindowDC(hWnd); // window is size of screen rect.right = Screen.PrimaryScreen.Bounds.Width; rect.bottom = Screen.PrimaryScreen.Bounds.Height; } else { hWnd = FindWindow(null, windowTitle); // get Win32 handle - must be free'ed later windowDC = GetWindowDC(hWnd); // work out size of window int returnCode = GetWindowRgnBox(hWnd, ref rect); } // create a bitmap to hold the picture Graphics g1 = Graphics.FromHdc(windowDC); Bitmap MyImage = new Bitmap(rect.right, rect.bottom, g1); Graphics g2 = Graphics.FromImage(MyImage); // get the device contexts dc1 = g1.GetHdc(); IntPtr dc2 = g2.GetHdc(); // ask Win32 to copy the bitmap on the screen to our bitmap BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc1, 0, 0, 13369376); // give back the handles g1.ReleaseHdc(dc1); g2.ReleaseHdc(dc2); // free up Win32 handle ReleaseDC(hWnd, windowDC); return MyImage; } [DllImportAttribute("gdi32.dll")] private static extern bool BitBlt( IntPtr hdcDest, // handle to destination DC int nXDest, // x-coord of destination upper-left corner int nYDest, // y-coord of destination upper-left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle IntPtr hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y-coordinate of source upper-left corner System.Int32 dwRop // raster operation code ); [DllImportAttribute("gdi32.dll")] private static extern IntPtr CreateDC( string lpszDriver, // driver name string lpszDevice, // device name string lpszOutput, // not used; should be NULL IntPtr lpInitData // optional printer data ); [DllImportAttribute("User32.dll")] private static extern IntPtr FindWindow( string lpClassName, // class name string lpWindowName // window name ); [DllImportAttribute("User32.dll")] private static extern IntPtr GetWindowDC( IntPtr hWnd // handle to window );

                L Offline
                L Offline
                Li kai Liu Angus
                wrote on last edited by
                #7

                Thanks your guys:) I'm playing around with philips's code now. looks pretty nice. When I tried to spot out the location of Win32windows.zip on gotdotnet.com, I got lost... not well organized as codeproject So, leppie, would you mind sending it to my mailbox at ykliu@email-home.com? Thanks again:) Li-kai Liu

                1 Reply Last reply
                0
                • L leppie

                  No, I was just wondering. :) In my experience Rectangle and the other Drawing structs have been fine for marshalling. I have seen people this alot though...even MS people :laugh: Give them a chance! Do it for the kittens, dear God, the kittens!

                  L Offline
                  L Offline
                  Li kai Liu Angus
                  wrote on last edited by
                  #8

                  are you saying we can re-write this [System.Runtime.InteropServices.DllImportAttribute("User32.dll")] private static extern int GetWindowRgnBox( IntPtr hWnd, // handle to window ref RECT rect // rectangle ); to [System.Runtime.InteropServices.DllImportAttribute("User32.dll")] private static extern int GetWindowRgnBox( IntPtr hWnd, // handle to window ref Rectangle rect // rectangle ); But wouldn't it be relatively confusing because the last two field actually mean different things (width, height in Rectangle, but right, bottom in RECT)...

                  L 1 Reply Last reply
                  0
                  • L Li kai Liu Angus

                    are you saying we can re-write this [System.Runtime.InteropServices.DllImportAttribute("User32.dll")] private static extern int GetWindowRgnBox( IntPtr hWnd, // handle to window ref RECT rect // rectangle ); to [System.Runtime.InteropServices.DllImportAttribute("User32.dll")] private static extern int GetWindowRgnBox( IntPtr hWnd, // handle to window ref Rectangle rect // rectangle ); But wouldn't it be relatively confusing because the last two field actually mean different things (width, height in Rectangle, but right, bottom in RECT)...

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    Li-kai Liu (Angus) wrote: are you saying we can re-write this No! I was wrong. Thanx for pointing it out. For some strange bizarre reason, my brain recorded wrong info :(( Or maybe I'm thinking of something completely different. I have tried to find it, but it was one of those snippets I wrote for someone, but never saved it. :mad: In fact, I found 16 blank WindowsApplictions in my code directory.:wtf::eek: CHeers :) Give them a chance! Do it for the kittens, dear God, the kittens!

                    1 Reply Last reply
                    0
                    • P Philip Fitzsimons

                      try this... public Bitmap CaptureWindow(string windowTitle) { RECT rect = new RECT(); IntPtr windowDC; IntPtr dc1; IntPtr hWnd; // get the window device context for drawing if (windowTitle == null) { hWnd = FindWindow(null, "DISPLAY"); // get Win32 handle - must be free'ed later windowDC = GetWindowDC(hWnd); // window is size of screen rect.right = Screen.PrimaryScreen.Bounds.Width; rect.bottom = Screen.PrimaryScreen.Bounds.Height; } else { hWnd = FindWindow(null, windowTitle); // get Win32 handle - must be free'ed later windowDC = GetWindowDC(hWnd); // work out size of window int returnCode = GetWindowRgnBox(hWnd, ref rect); } // create a bitmap to hold the picture Graphics g1 = Graphics.FromHdc(windowDC); Bitmap MyImage = new Bitmap(rect.right, rect.bottom, g1); Graphics g2 = Graphics.FromImage(MyImage); // get the device contexts dc1 = g1.GetHdc(); IntPtr dc2 = g2.GetHdc(); // ask Win32 to copy the bitmap on the screen to our bitmap BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc1, 0, 0, 13369376); // give back the handles g1.ReleaseHdc(dc1); g2.ReleaseHdc(dc2); // free up Win32 handle ReleaseDC(hWnd, windowDC); return MyImage; } [DllImportAttribute("gdi32.dll")] private static extern bool BitBlt( IntPtr hdcDest, // handle to destination DC int nXDest, // x-coord of destination upper-left corner int nYDest, // y-coord of destination upper-left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle IntPtr hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y-coordinate of source upper-left corner System.Int32 dwRop // raster operation code ); [DllImportAttribute("gdi32.dll")] private static extern IntPtr CreateDC( string lpszDriver, // driver name string lpszDevice, // device name string lpszOutput, // not used; should be NULL IntPtr lpInitData // optional printer data ); [DllImportAttribute("User32.dll")] private static extern IntPtr FindWindow( string lpClassName, // class name string lpWindowName // window name ); [DllImportAttribute("User32.dll")] private static extern IntPtr GetWindowDC( IntPtr hWnd // handle to window );

                      L Offline
                      L Offline
                      Li kai Liu Angus
                      wrote on last edited by
                      #10

                      BTW, is that possible to include the cursor in the captured image??

                      P 1 Reply Last reply
                      0
                      • L Li kai Liu Angus

                        BTW, is that possible to include the cursor in the captured image??

                        P Offline
                        P Offline
                        Philip Fitzsimons
                        wrote on last edited by
                        #11

                        I don't believe so, its handled by windows.


                        "When the only tool you have is a hammer, a sore thumb you will have."

                        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