Solved

Problem with Screen Capture

Posted on 2004-09-10
4
626 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 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