屏幕捕获不捕获我的应用程序中的对话框

时间:2015-02-24 05:00:11

标签: c# wpf winforms

我有一个在WPF和WinForms中创建的应用程序A.我在WinForms中为捕获屏幕编写了另一个应用程序。我面临的问题是,应用程序A中出现的对话框未在屏幕中捕获。捕获整个屏幕,包括对话框后面的区域,但不会捕获对话框。

    public void CaptureScreen(string filepath)
    {

        string[] words = filepath.Split('\\');
        string newFilePath = " ";
        foreach (string word in words)
        {
            if (!(word.Contains(".bmp")))
            {
                newFilePath = newFilePath + word + "//";
            }
            else
            {
                newFilePath = newFilePath + word;
            }


        }

        this.WindowState = FormWindowState.Minimized;


        Screen[] screens;
        screens = Screen.AllScreens;
        int noofscreens = screens.Length, maxwidth = 0, maxheight = 0;
        for (int i = 0; i < noofscreens; i++)
        {
            if (maxwidth < (screens[i].Bounds.X + screens[i].Bounds.Width)) maxwidth = screens[i].Bounds.X + screens[i].Bounds.Width;
            if (maxheight < (screens[i].Bounds.Y + screens[i].Bounds.Height)) maxheight = screens[i].Bounds.Y + screens[i].Bounds.Height;
        }


        var width = maxwidth;
        var height = maxheight;

        Point sourcePoint = Point.Empty;
        Point destinationPoint = Point.Empty;

        Rectangle rect = new Rectangle(0, 0, width, height);

        Bitmap bitmap = new Bitmap(rect.Width, rect.Height);

        Graphics g = Graphics.FromImage(bitmap);

       // g.CopyFromScreen(sourcePoint, destinationPoint, rect.Size);

        g.CopyFromScreen(new Point(rect.Left, rect.Top), Point.Empty, rect.Size);

        bitmap.Save(filepath, ImageFormat.Bmp);


        //Console.WriteLine("(width, height) = ({0}, {1})", maxx - minx, maxy - miny);


    }


}

}

1 个答案:

答案 0 :(得分:0)

 Bitmap bmpScreenshot = new Bitmap(Screen.AllScreens[1].Bounds.Width, Screen.AllScreens[1].Bounds.Height, PixelFormat.Format32bppArgb);

            Graphics.FromImage(bmpScreenshot).CopyFromScreen(
                                         Screen.AllScreens[1].Bounds.X,
                                         Screen.AllScreens[1].Bounds.Y,
                                         0,
                                         0,
                                         Screen.AllScreens[1].Bounds.Size,
                                         CopyPixelOperation.SourceCopy);

            this.picExtendedModitorScreen.Image = bmpScreenshot;
            this.picExtendedModitorScreen.Refresh();

将此代码放入计时器刻度事件中。

我已将扩展屏幕1放在代码中,您可以将其更改为任何其他代码。