I'm trying to capture browser screenshot and one of my Win32 api method is GetWindowRect. This is returning same left & right value. This is only happen when I'm running my application in a remote machine having Win7 as a OS.
Also my PrintWindow method failing in this machine. If anyone have faced this issue before please let me know.
Those above two methods works fine with Vista and XP as OS in remote machine.
Adding few of the methods of my application.
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
private Image Capture(IntPtr hwnd)
{
Rectang开发者_运维知识库le windowSize = this.GetWindowPosition(hwnd);
Bitmap bm = new Bitmap(windowSize.Width, windowSize.Height);
using (Graphics g = Graphics.FromImage(bm))
{
IntPtr hdc = g.GetHdc();
if (PrintWindow(hwnd, hdc, 0) == false)
{
throw new Exception("PrintWindow call failed");
}
g.ReleaseHdc(hdc);
g.Flush();
}
return bm;
}
private Rectangle GetWindowPosition(IntPtr hwnd)
{
Rect r = new Rect();
GetWindowRect(hwnd, ref r);
return new Rectangle(r.Left, r.Top, r.Width, r.Height);
}
You aren't checking your Win32 return codes. My guess is that GetWindowRect
fails for some reason and so doesn't assign any values to the rect. Thus its values remain uninitialised.
Check the return value and if the call fails use Marshal.GetLastWin32Error()
to find out why. You'll need to update your P/Invokes too:
[DllImport("user32.dll", SetLastError=true)]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);
[DllImport("user32.dll", SetLastError=true)]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
...
if (!GetWindowRect(hwnd, ref r))
int ErrorCode = Marshal.GetLastWin32Error();
精彩评论