Solved

Problem with Screen Capture

Posted on 2004-09-10
4
623 Views
Last Modified: 2012-06-27
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)?
0
Comment
Question by:rmyhre
4 Comments
 
LVL 24

Assisted Solution

by:Justin_W
Justin_W earned 250 total points
ID: 12031358
It sounds like maybe a memory or resource problem.  I would try to vary the size of the images and/or the frequency of capture and see if the 45 changes to something else.  If so, you should try to track down what sort of resource is being leaked.
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 12036825
0
 
LVL 4

Accepted Solution

by:
thedude112286 earned 250 total points
ID: 12039958
You have a memory problem.  You must release each DC that you use.

ReleaseDC(handle, hdcSrc);
DeleteDC(hdcDest);
DeleteObject(hBitmap);

It looks like your only missing that last one: the DeleteObject call.
0
 

Author Comment

by:rmyhre
ID: 12044719
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now