WPF - 黑屏而不是屏幕截图

时间:2013-07-20 14:21:39

标签: wpf screen-scraping screenshot

我使用以下代码获取屏幕截图:

    public static BitmapSource ToBitmapSource()
    {
        using (var screenBmp = new Bitmap(Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenWidth), 
            Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenHeight), 
            System.Drawing.Imaging.PixelFormat.Format32bppArgb))
        {
            using (var bmpGraphics = Graphics.FromImage(screenBmp))
            {
                bmpGraphics.CopyFromScreen(0, 0, 0,
                    0, screenBmp.Size);
                return Imaging.CreateBitmapSourceFromHBitmap(screenBmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            }
        }
    }

它适用于普通窗口,但我在FullScreen应用程序上获得黑色矩形而不是屏幕截图。为什么以及如何修复? 感谢

1 个答案:

答案 0 :(得分:1)

您提供的代码准确无误。我认为在保存屏幕截图时遇到问题。所以我在这里附上了我的测试结果

private void Button_Click(object sender, RoutedEventArgs e)
{
    PngBitmapEncoder png = new PngBitmapEncoder();
    png.Frames.Add(BitmapFrame.Create(ToBitmapSource()));    // Calling your method
    using (Stream stm = File.Create(AppDomain.CurrentDomain.BaseDirectory + "screenshot.png"))
    {
        png.Save(stm);
    }
}

enter image description here