Python程序抓图 怎么用程序实现截图

Python程序抓图 怎么用程序实现截图,第1张

由于在我的一个程序中想要添加一个截图功能,今天看一下利用Python怎样截图,功能实现都挺简单了,直接上代码

from

PIL

import

ImageGrab

im

=

ImageGrab.grab()

im.save(addr,'jpeg')

很简单的几行代码就实现了我要的功能,PIL(Python

Image

Library)是Python的一个图形库,需要自己下载安装,im

=

ImageGrab.grab()这行代码实现截图功能,可以带参数,指定要截取图片的坐标位置,不带参数默认全屏截图,im.save(addr,'jpeg')是保存截取的图片,第一个参数是保存路径,第二个参数是图片格式

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)

        }


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存