Solved

Problem with Screen Capture

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

16 Experts available now in Live!

Get 1:1 Help Now