管理

窗体图像操作类 - C#小函数类推荐

Posted on 2025-02-10 00:00  lzhdim  阅读(10098)  评论(0)    收藏  举报

       此文记录的是窗体截图工具类。

/***

    窗体图像操作类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   2024-01-15 15:18:00

    使用方法:
        Image image = WindowImageUtil.GetWindowImage(this.Handle);

    注意:
        截图的包括遮挡窗口,最小化窗口。

***/

namespace Lzhdim.LPF.Utility
{
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    /// <summary>
    /// 窗体图像操作类
    /// </summary>
    public class WindowImageUtil
    {
        /// <summary>
        /// 获取窗体当前截图
        /// </summary>
        /// <param name="windownHandle"></param>
        /// <returns></returns>
        public static Image GetWindowImage(IntPtr windownHandle)
        {
            Control control = Control.FromHandle(windownHandle);
            Bitmap image = new Bitmap(control.Width, control.Height);
            Graphics gp = Graphics.FromImage(image);
            IntPtr dc = gp.GetHdc();
            PrintWindow(windownHandle, dc, 0);
            gp.ReleaseHdc();
            gp.Dispose();
            return image;
        }

        [DllImport("user32.dll")]
        private static extern bool PrintWindow(
         IntPtr hwnd,               // Window to copy,Handle to the window that will be copied.
         IntPtr hdcBlt,             // HDC to print into,Handle to the device context.
         UInt32 nFlags              // Optional flags,Specifies the drawing options. It can be one of the following values.
         );
    }
}

 

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved