Link to home
Start Free TrialLog in
Avatar of rmyhre
rmyhre

asked on

Problem with Screen Capture

This is in conjunction with another post I have trying to get a screen capture program working.

My problem at this point is that at exactly 45 captures, it stops capturing any more data.  Can someone point out a problem with this code:

public class ScreenScraper
      {
            public static int handle = 0;

            #region DLLImports

            [ DllImport("user32.dll") ]
            public static extern int GetForegroundWindow();

            [DllImport("GDI32.dll")]
            public static extern bool BitBlt(IntPtr hdcDest,int nXDest,int nYDest,
                  int nWidth,int nHeight,IntPtr hdcSrc,
                  int nXSrc,int nYSrc,int dwRop);

            [DllImport("GDI32.dll")]
            public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,int nWidth,
                  int nHeight);

            [DllImport("GDI32.dll")]
            public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

            [DllImport("GDI32.dll")]
            public static extern bool DeleteDC(IntPtr hdc);

            [DllImport("GDI32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);

            [DllImport("GDI32.dll")]
            public static extern int GetDeviceCaps(IntPtr hdc,int nIndex);

            [DllImport("GDI32.dll")]
            public static extern int SelectObject(IntPtr hdc,IntPtr hgdiobj);

            [DllImport("User32.dll")]
            public static extern IntPtr GetWindowDC(int hWnd);

            [DllImport("User32.dll")]
            public static extern int ReleaseDC(int hWnd,IntPtr hDC);

            #endregion

            public ScreenScraper()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            public static Bitmap GetImage(int StartX)
            {
                  IntPtr hdcSrc = GetWindowDC(handle);
                  IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
                  IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, GetDeviceCaps(hdcSrc,8),
                        GetDeviceCaps(hdcSrc, 10));
                  if (hBitmap != IntPtr.Zero)
                  {
                        SelectObject(hdcDest, hBitmap);
                        BitBlt(hdcDest, 0,0, 55, 75, hdcSrc, StartX, 185, 0x00CC0020);  //SRC_COPY = 13369376
                        ReleaseDC(handle, hdcSrc);
                        DeleteDC(hdcDest);
                        return System.Drawing.Image.FromHbitmap(hBitmap);
                  }
                  return null;
            }

            public static void GetActiveWindow()
            {
                     handle = GetForegroundWindow();
            }
            }

I wonder why it stops at exactly 45 each time (I used a timer to place the captures into a picturebox every 500 ms)?
SOLUTION
Avatar of Justin_W
Justin_W

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmyhre
rmyhre

ASKER

Yeah, I tried that as well.  It still had problems.  If I moved the variables to a global nature, I could capture more times but then it eventually ran out of memory.

It appears that it has something to do with the PictureBox itself.  I stopped using it and went straight to the comparisons and I haven't had any problems.