从Firefox进程使用MainWindowHandle调用时,GetWindowRect在每个属性上返回一个Rect,每个属性都为0

时间:2019-05-01 18:36:14

标签: c# firefox rect user32

我需要使用Winforms应用程序从firefox中截取屏幕截图,并使用GetWindowRect并从该处基于Rect创建位图来执行此操作。矩形具有0下,0上,0左和0右。对于其他任务,例如资源管理器也可以正常工作。

我首先认为firefox的进程不是主窗口,所以我开始了一些新的尝试,并尝试从我的数组中获取其他名为firefox的进程,但错误始终相同。

 Bitmap screenshot = CaptureApplication("firefox");

 public Bitmap CaptureApplication(string procName)
    {

        var t = Task.Run(() =>
        {
  //getting the first process with the name firefox
        var proc = Process.GetProcessesByName(procName)[0]; 
//Get a rect in the size of the firefox process
        var rect = new User32.Rect();
        User32.GetWindowRect(proc.MainWindowHandle, ref rect);
            return rect;
        });
//Make screenshot based on the size of the rect (ante)
       var t2= t.ContinueWith((ante) =>
        {
            var rect = ante.Result;

        int width = rect.right - rect.left; //right and left are 0
        int height = rect.bottom - rect.top; //bottom and top are 0
        //here is the error
        var bmp = new Bitmap(width, height,   PixelFormat.Format32bppArgb); //cant create a Bitmap with 0,0 as parameter
        Graphics graphics = Graphics.FromImage(bmp);
        graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
            return bmp;
        });
        return t2.Result;
    }
 public class User32
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Rect
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
}

我只想要firefox的屏幕截图,但是只要该矩形的每个属性都返回0,该程序就不会运行。

0 个答案:

没有答案