C# png格式的桌面捕获

C# png格式的桌面捕获,c#,wpf,screen-capture,C#,Wpf,Screen Capture,我想得到一个屏幕截图,并保存在整个屏幕的png格式。我该怎么做 我可以使用剪贴工具库来完成这项工作吗?internet上有一些教程向您展示如何使用windows窗体执行此操作,并且图像的格式为位图 这里有一个捕获任何屏幕内容的小方法 private static void CaptureScreen(Screen window, string file) { try { Rectangle s_rect = window.B

我想得到一个屏幕截图,并保存在整个屏幕的png格式。我该怎么做


我可以使用剪贴工具库来完成这项工作吗?internet上有一些教程向您展示如何使用windows窗体执行此操作,并且图像的格式为位图

这里有一个捕获任何屏幕内容的小方法

    private static void CaptureScreen(Screen window, string file)
    {
        try
        {
            Rectangle s_rect = window.Bounds;
            using (Bitmap bmp = new Bitmap(s_rect.Width, s_rect.Height))
            {
                using (Graphics gScreen = Graphics.FromImage(bmp))
                    gScreen.CopyFromScreen(s_rect.Location, Point.Empty, s_rect.Size);
                bmp.Save(file, System.Drawing.Imaging.ImageFormat.Png);
            }
        }
        catch (Exception) { /*TODO: Any exception handling.*/ }
    }
用法示例:

 CaptureScreen(Screen.PrimaryScreen, @"B:\exampleScreenshot.png");
编辑:稍后回到这里,我意识到从函数中返回
图像
对象可能更有用,这样您就可以选择如何使用捕获的位图

我还使该功能更加健壮,因此它可以捕获多个屏幕(即在多监视器设置中)。它应该能适应不同高度的屏幕,但我自己无法测试

    public static Image CaptureScreens(params Screen[] screens) {
    if (screens == null || screens.Length == 0)
        throw new ArgumentNullException("screens");

    // Order them in logical left-to-right fashion.
    var orderedScreens = screens.OrderBy(s => s.Bounds.Left).ToList();
    // Calculate the total width needed to fit all the screen into a single image
    var totalWidth = orderedScreens.Sum(s => s.Bounds.Width);
    // In order to handle screens of different sizes, make sure to make the Bitmap large enough to fit the tallest screen
    var maxHeight = orderedScreens.Max(s => s.Bounds.Top + s.Bounds.Height);

    var bmp = new Bitmap(totalWidth, maxHeight);
    int offset = 0;

    // Copy each screen to the bitmap
    using (var g = Graphics.FromImage(bmp)) {
        foreach (var screen in orderedScreens) {
            g.CopyFromScreen(screen.Bounds.Left, screen.Bounds.Top, offset, screen.Bounds.Top, screen.Bounds.Size);
            offset += screen.Bounds.Width;
        }
    }

    return bmp;
}
新例子:

// Capture all monitors and save them to file
CaptureScreens(Screen.AllScreens).Save(@"C:\Users\FooBar\screens.png");

下面是一个捕获任何屏幕内容的小方法

    private static void CaptureScreen(Screen window, string file)
    {
        try
        {
            Rectangle s_rect = window.Bounds;
            using (Bitmap bmp = new Bitmap(s_rect.Width, s_rect.Height))
            {
                using (Graphics gScreen = Graphics.FromImage(bmp))
                    gScreen.CopyFromScreen(s_rect.Location, Point.Empty, s_rect.Size);
                bmp.Save(file, System.Drawing.Imaging.ImageFormat.Png);
            }
        }
        catch (Exception) { /*TODO: Any exception handling.*/ }
    }
用法示例:

 CaptureScreen(Screen.PrimaryScreen, @"B:\exampleScreenshot.png");
编辑:稍后回到这里,我意识到从函数中返回
图像
对象可能更有用,这样您就可以选择如何使用捕获的位图

我还使该功能更加健壮,因此它可以捕获多个屏幕(即在多监视器设置中)。它应该能适应不同高度的屏幕,但我自己无法测试

    public static Image CaptureScreens(params Screen[] screens) {
    if (screens == null || screens.Length == 0)
        throw new ArgumentNullException("screens");

    // Order them in logical left-to-right fashion.
    var orderedScreens = screens.OrderBy(s => s.Bounds.Left).ToList();
    // Calculate the total width needed to fit all the screen into a single image
    var totalWidth = orderedScreens.Sum(s => s.Bounds.Width);
    // In order to handle screens of different sizes, make sure to make the Bitmap large enough to fit the tallest screen
    var maxHeight = orderedScreens.Max(s => s.Bounds.Top + s.Bounds.Height);

    var bmp = new Bitmap(totalWidth, maxHeight);
    int offset = 0;

    // Copy each screen to the bitmap
    using (var g = Graphics.FromImage(bmp)) {
        foreach (var screen in orderedScreens) {
            g.CopyFromScreen(screen.Bounds.Left, screen.Bounds.Top, offset, screen.Bounds.Top, screen.Bounds.Size);
            offset += screen.Bounds.Width;
        }
    }

    return bmp;
}
新例子:

// Capture all monitors and save them to file
CaptureScreens(Screen.AllScreens).Save(@"C:\Users\FooBar\screens.png");