GetWindowRect возвращает Rect с 0 на каждое свойство при вызове с MainWindowHandle из процесса, который является Firefox - PullRequest
0 голосов
/ 01 мая 2019

Мне нужно сделать скриншот из Firefox с приложением Winforms, но с помощью 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 для каждого свойства прямоугольника.

...