C# 获取桌面

本文详细介绍了一种使用 C# 进行屏幕截图的技术实现方法。通过调用 GDI+ 库中的多个 API 函数,如 CreateDC、CreateCompatibleDC 和 BitBlt 等,实现了从桌面获取指定区域的图像,并将其转换为 Bitmap 对象的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

              

 [System.Runtime.InteropServices.DllImport(”gdi32.dll”)]

        public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, UInt32 dwRop);

      //创建桌面句柄

        [System.Runtime.InteropServices.DllImportAttribute(”gdi32.dll”)]

        public static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, int lpInitData);

        [System.Runtime.InteropServices.DllImport(”gdi32.dll”)]

        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        //转换为本地的图像资源

        [System.Runtime.InteropServices.DllImport(”gdi32.dll”)]

        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

        [System.Runtime.InteropServices.DllImport(”gdi32.dll”)]

        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        [System.Runtime.InteropServices.DllImport(”gdi32.dll”)]

        public static extern int DeleteDC(IntPtr hdc);

        //释放用过的设备句柄

        [DllImport(”user32.dll”)]

        public static extern bool ReleaseDC(

         IntPtr hwnd, IntPtr hdc

         );

        //释放用过的画笔等资源

        [DllImport(”gdi32.dll”)]

        public static extern bool DeleteObject(

          IntPtr hdc

         );

        

/// <summary>

        /// 截取屏幕图像

        /// </summary>

        /// <param name=”Width”>宽</param>

        /// <param name=”Height”>高</param>

        /// <param name=”x”>x坐标(全屏时候为0)</param>

        /// <param name=”y”>y坐标(全屏时候为0)</param>

        /// <returns></returns>

        public Bitmap fullphoto(int Width,int Height,int x,int y)

        {

            Bitmap bitmap;

            //try

            //{

                IntPtr hScreenDc = CreateDC(”DISPLAY”, null, null, 0); // 创建桌面句柄

                IntPtr hMemDc = CreateCompatibleDC(hScreenDc); // 创建与桌面句柄相关连的内存DC

                IntPtr hBitmap = CreateCompatibleBitmap(hScreenDc, Width, Height);   

                IntPtr hOldBitmap = SelectObject(hMemDc, hBitmap);

                BitBlt(hMemDc, x, y, Width, Height, hScreenDc, x, y, (UInt32)0xcc0020);

                IntPtr map = SelectObject(hMemDc, hOldBitmap);

                bitmap = Bitmap.FromHbitmap(map);  

                ReleaseDC(hBitmap, hScreenDc);

                DeleteDC(hScreenDc);//删除用过的对象

                DeleteDC(hMemDc);//删除用过的对象

                DeleteDC(hOldBitmap);

                DeleteObject(hBitmap);

               

              

            //}

            //catch (Exception wx)

            //{

            //    return null;

                //}

                // number= number +1;

                // bitmap.Save(”screen” + number + “.bmp”);

            

            return bitmap;

        }

       

转载于:https://www.cnblogs.com/DotNetCSharp/archive/2011/07/25/2116574.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值