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(Int Ptr 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(hdc Src, 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.FromH bitmap(hBi tmap);
}
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)?
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(Int
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(hdc
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.FromH
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
Look at here..
http://www.thecodeproject.com/csharp/ImageCapture.asp
http://www.thecodeproject.com/csharp/cscapturescreen1.asp
i hope it may help you..
R.K