本文介绍了WPF中的桌面屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在WPF中拍摄桌面截图?最好显示鼠标光标。

How can I take a screenshot of the desktop in WPF? Preferably with the mouse cursor showing.

推荐答案

在不试图窃取答案的情况下,请使用Johannes引用的CodeProject文章中给出的代码创建GDI位图。然后,您可以使用以下代码将其转换为可在WPF中使用的BitmapSource:

Without trying to steal the answer, use the code give in the CodeProject article referenced by Johannes to create the GDI bitmap. You can then use the following code to convert it into a BitmapSource for use in WPF:

    public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap source)
    {
        var hBitmap = source.GetHbitmap();

        try
        {
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }
        catch (Win32Exception)
        {
            return null;
        }
        finally
        {
            NativeMethods.DeleteObject(hBitmap);
        }
    }

其中NativeMethods.DeleteObject()的代码为:

where the code for NativeMethods.DeleteObject() is:

    [DllImport("gdi32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool DeleteObject(IntPtr hObject);

这篇关于WPF中的桌面屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:44