再C#中QQ截图的代码怎么写

再C#中QQ截图的代码怎么写,第1张

QQ截图的核心其实就是调用WINDOWS API函数,主要涉及两个核心组件, user32.dll和gdi32.dll。

如下是,C#代码调用上述两个核心组件的完整示例:

namespace WindowsFormsApplication1

{

    /// <summary>

    /// 屏幕捕获类 

    /// </summary>

    public class ScreenCapture

    {

        /// <summary>

        /// 创建一个包含整个桌面的截图Image对象(捕获到的桌面是当前WINDOWS *** 作系统活动桌面)

        /// </summary>

        /// <returns></returns>

        public Image CaptureScreen()

        {

            return CaptureWindow(User32.GetDesktopWindow())

        }

        /// <summary>

        /// 创建一个包含特定窗口的截图Image对象

        /// </summary>

        /// <param name="handle">启动本程序的句柄窗口(在Windows上这是由Handle属性获得)</param>

        /// <returns></returns>

        public Image CaptureWindow(IntPtr handle)

        {

            // 获取目标窗口的HDC

            IntPtr hdcSrc = User32.GetWindowDC(handle)

            // 获取它的大小

            User32.RECT windowRect = new User32.RECT()

            User32.GetWindowRect(handle, ref windowRect)

            int width = windowRect.right - windowRect.left

            int height = windowRect.bottom - windowRect.top

            // 创建设备上下文对象

            IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc)

            IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height)

            IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap)

            GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY)

            GDI32.SelectObject(hdcDest, hOld)

            GDI32.DeleteDC(hdcDest)

            User32.ReleaseDC(handle, hdcSrc)

            Image img = Image.FromHbitmap(hBitmap)

            GDI32.DeleteObject(hBitmap)

            return img

        }

        /// <summary>

        /// 获取特定窗口,并保存它

        /// </summary>

        /// <param name="handle"></param>

        /// <param name="filename"></param>

        /// <param name="format"></param>

        public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)

        {

            Image img = CaptureWindow(handle)

            img.Save(filename, format)

        }

        /// <summary>

        /// 捕获整个windows活动窗口并保存它

        /// </summary>

        /// <param name="filename"></param>

        /// <param name="format"></param>

        public void CaptureScreenToFile(string filename, ImageFormat format)

        {

            Image img = CaptureScreen()

            img.Save(filename, format)

        }

        /// <summary>

        /// GDI32 相关的API函数

        /// </summary>

        private class GDI32

        {

            public const int SRCCOPY = 0x00CC0020  

            [DllImport("gdi32.dll")]

            public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,

                int nWidth, int nHeight, IntPtr hObjectSource,

                int nXSrc, int nYSrc, int dwRop)

            [DllImport("gdi32.dll")]

            public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,

                int nHeight)

            [DllImport("gdi32.dll")]

            public static extern IntPtr CreateCompatibleDC(IntPtr hDC)

            [DllImport("gdi32.dll")]

            public static extern bool DeleteDC(IntPtr hDC)

            [DllImport("gdi32.dll")]

            public static extern bool DeleteObject(IntPtr hObject)

            [DllImport("gdi32.dll")]

            public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject)

        }

        /// <summary>

        /// User32 API相关函数

        /// </summary>

        private 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 GetDesktopWindow()

            [DllImport("user32.dll")]

            public static extern IntPtr GetWindowDC(IntPtr hWnd)

            [DllImport("user32.dll")]

            public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC)

            [DllImport("user32.dll")]

            public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect)

        }

    }

}

//  调用示例:

private void button1_Click(object sender, EventArgs e)

        {

            ScreenCapture sc = new ScreenCapture()

            // 捕获整个屏幕并保存到一个文件里

            Image img = sc.CaptureScreen()

            // 将捕获的图片显示在图片控件里

            this.pictureBox1.Image = img

            // 捕获当前运行窗体并保存在C盘,文件名和后缀为temp.png

            sc.CaptureWindowToFile(this.Handle, "C:\\temp.png", ImageFormat.Gif)

        }

这是窗口1的代码,半透明可以调用api.懒得查了.

.版本 2

.支持库 eAPI

.支持库 spec

.子程序 _按钮1_被单击

.局部变量 矩形数据, 矩形数据

窗口1.可视 = 假

矩形数据.左边 = 左边

矩形数据.顶边 = 顶边

矩形数据.右边 = 左边 + 宽度

矩形数据.底边 = 顶边 + 高度

延迟 (500)

底图 = 截取屏幕区域 (矩形数据, #接口常量.到字节集, )

可视 = 真

.子程序 _窗口1_创建完毕

边框 = 0

随意移动 = 真


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7772561.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存