?
Solved

Problem with Screen Capture

Posted on 2004-09-10
4
Medium Priority
?
630 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 24

Assisted Solution

by:Justin_W
Justin_W earned 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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