Randomly capturing "Black" screen

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi
I am creating a program to hook the messages. Its partially working. The capture function is sometimes capturing "black screen" instead of capturing fully message. (security message/security warning)
Please refer to the codes:
This is the CaptureScreen codes

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Drawing.Imaging;


<span style="color:Blue; namespace GlobalDialogHooking
{

<span style="color:Blue; public <span style="color:Blue; class CaptureScreen
{
<span style="color:Blue; #region Public Class Functions
<span style="color:Blue; public <span style="color:Blue; static Bitmap GetDesktopImage(IntPtr handler)
{
SIZE size; <span style="color:Green; // store the size of security window

RECT rect = <span style="color:Blue; new RECT();
WINDOWINFO pwi = <span style="color:Blue; new WINDOWINFO();
PlatformInvokeUSER32.GetWindowInfo(handler, <span style="color:Blue; out pwi); <span style="color:Green; //Get window information
rect = pwi.rcWindow;
size.cx = rect.right - rect.left;
size.cy = rect.bottom - rect.top;

<span style="color:Green; //Variable to keep the handle to bitmap.
Bitmap bitmap = <span style="color:Blue; null;
<span style="color:Green; //Here we get the handle to the window device context
IntPtr hdc = PlatformInvokeUSER32.GetWindowDC(handler);

<span style="color:Blue; if (hdc != IntPtr.Zero)
{
<span style="color:Green; //Here we make a compatible device context in memory for screen device context.
IntPtr hdcMem = PlatformInvokeGDI32.CreateCompatibleDC(hdc);
<span style="color:Blue; if (hdcMem != IntPtr.Zero)
{
<span style="color:Green; //Here we make a compatible device context in memory for screen device context.
IntPtr hbitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hdc, size.cx, size.cy);
<span style="color:Blue; if (hbitmap != IntPtr.Zero)
{
<span style="color:Green; //Here we select the compatible bitmap in memeory device context and keeps the refrence to Old bitmap.
IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject(hdcMem, hbitmap);
<span style="color:Green; //Updates the specified region in a windows client area.
<span style="color:Green; //RedrawWindow function can prevent capturing black screen when it uses PrintWindow
PlatformInvokeUSER32.RedrawWindow(handler, IntPtr.Zero, IntPtr.Zero, RedrawWindowFlags.Frame | RedrawWindowFlags.UpdateNow | RedrawWindowFlags.Invalidate);
<span style="color:Green; //We copy the Bitmap to the memory device context. Use PrintWindow API
PlatformInvokeUSER32.PrintWindow(handler, hdcMem, 0);
<span style="color:Green; //Image is created by Image bitmap handle and stored in local variable.
bitmap = System.Drawing.Image.FromHbitmap(hbitmap);
<span style="color:Green; //We select the old bitmap back to the memory device context.
PlatformInvokeGDI32.SelectObject(hdcMem, hOld);
<span style="color:Green; //Release the memory to avoid memory leaks.
PlatformInvokeGDI32.DeleteObject(hbitmap);
}
<span style="color:Green; //We delete the memory device context.
PlatformInvokeGDI32.DeleteObject(hdcMem);
}
<span style="color:Green; //We release the screen device context.
PlatformInvokeUSER32.ReleaseDC(handler, hdc);
}
<span style="color:Blue; return bitmap;
}
<span style="color:Blue; #endregion
[/code]

This is ScreenShotSave codes

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Reflection;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Drawing.Imaging;
<span style="color:Blue; using System.Windows.Forms;

<span style="color:Blue; namespace GlobalDialogHooking
{
<span style="color:Blue; class ScreenshotSave
{
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; bool SaveScreenshot( <span style="color:Blue; int fileName, IntPtr handler )
{
<span style="color:Blue; string currentFilePath = Assembly.GetExecutingAssembly().Location.Substring(0, Assembly.GetExecutingAssembly().Location.LastIndexOf(\)) + <span style="color:#A31515; "\Images\" + fileName.ToString()+".jpg";
PictureBox curPic = <span style="color:Blue; new PictureBox();
curPic.Image=CaptureScreen.GetDesktopImage(handler);
<span style="color:Green; // Bitmap curPic = CaptureScreen.GetDesktopImage(handler);
<span style="color:Blue; try
{
curPic.Image.Save(currentFilePath);
<span style="color:Green; // curPic.Save(currentFilePath, ImageFormat.Jpeg);
}
<span style="color:Blue; catch (Exception ex)
{
MessageBox.Show(<span style="color:#A31515; "Cant save screenshot: "+ex.Message.ToString());
<span style="color:Blue; return <span style="color:Blue; false;
}
<span style="color:Blue; return <span style="color:Blue; true;
}
}
}

[/code]
<br/>
This is the bit where it call the function to detect the security message and later


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; if (aArgs.WindowClass == <span style="color:#A31515; "MozillaDialogClass")
{
curHandler = aArgs.Handle;
curAppName = <span style="color:#A31515; "Firefox";
ScreenshotSave.SaveScreenshot(DataUtility.GetImageID() + 1, aArgs.Handle);
}
[/code]

<br/>
Problems:

I have read that API doesnt work well with WS_EX_LAYERED window style.

source: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/3e3decd8-ced1-4f17-a745-466e5aa91391/ http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/3e3decd8-ced1-4f17-a745-466e5aa91391/
But still unable to find solution.
Sometimes this program able to capture proper image(full coverage security message) and sometime it will capture black screen only. I dont know why. If anyone could help/advise me on this would be very much appreciated. <hr class="sig zafi

View the full article
 
Back
Top