再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、登陆电脑版qq,在朋友列表下随意双击一个qq好友。

2、点击聊天窗口中的剪刀工具,或按快捷键“Ctrl + Alt + A”。

3、皮扰将鼠标移动到桌面上其他地方,就会看到如下的界面。

4、按住鼠标左键,在桌面上由左上到右下拖动出一个方框来,方框鼠标时颂蔽,就会看到除了圈出了一个矩形框的图片,图片下方还有一个菜单。

5、点击菜单上的“保存”燃樱旦,可以直接将图片保存到指定文件夹下。


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存