C#关于GDI+绘图(五)

一、截取屏幕图像

     在Windows中可以使用PrintScreen键进行截屏操作,将屏幕图像保存到剪贴板中,然后可以在其他应用程序,如“画图板”中进行处理,也可以直接粘贴到其他应用程序中。

新建一个Windows窗体应用程序,命名为CaptureScreen。新建一个类,命名为PlatFormInvokeGDI32.cs:

using System;

using System.Runtime.InteropServices;

namespace CaptureScreen

{

    ///<summary>

    ///引入一些GDI32中的方法

    ///</summary>

    public class platformInvokeGDI32

    {

         #region Class Variables

         public const int SRCCOPY = 13369376;

         #endregion


         #region Class Functions

         [DllImport("gdi32.dll",EntryPoint="DeleteDC")]

         public static extern IntPtr DeleteDC(IntPtr hDc);


         

         [DllImport("gdi32.dll",EntryPoint="Deleteobject")]

         public static extern IntPtr Deleteobject(IntPtr hDc);


         [DllImport("gdi32.dll",EntryPoint ="BitBlt")]

         public static extern bool BitBlt(IntPtr hdcDest,int xDest,int yDest,int wDest,int hDest,IntPtr hdcSource,int xSrc,int ySrc,int RasterOp);


         [DllImport("gdi32.dll",EntryPoint = "CreateCompatibleBitmap")]

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


         [DllImport("gdi32.dll",EntryPoint = "CreateCompatibleDC")]

         public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

  

         [DllImport("gdi32.dll",EntryPoint ="SelectObject")]

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

         #endregion


     #region Public Constructor

     public PlatformInvokeGDI32()

     {

     }

     #endregion

    }

}


新建一个类,命名为PlatformInvokeUSER32.cs

using System;

using System.Runtime.InteropServices;


namespace CaptureScreen

{

    ///<summary>

    ///引入一些User32中的方法

    ///</summary>

    public class platformInvokeUSER32

    {

       #region Class Variables

       public const int SM_CXSCREEN=0;

       public const int SM_CYSCREEN=1;

       #endregion


       #region Class Functions

       [DllImport("user32.dll",EntryPoint="GetDesktopWindow")]

       public static extern Intptr GetDesktopWindow();


       [DllImport("user32.dll",EntryPoint="GetDC")]

       public static extern Intptr GetDC(Intptr ptr);


       [DllImport("user32.dll",EntryPoint="GetSystemMetrics")]

       public static extern int GetSystemMetrics(int abc);


       [DllImport("user32.dll",EntryPoint="GetWindowDC")]

       public static extern Intptr GetWindowDC(Int32 ptr);


       [DllImport("user32.dll",EntryPoint="ReleaseDC")]

       public static extern Intptr ReleaseDC(Intptr hWnd,Intptr hDc);

       #endregion


       #region public Constructor

       public PlatformInvokeUSER32()

       {

       }

       #endregion

    }

    ///<summary>

    ///表示屏幕大小

    ///</summary>

    public struct SIZE

    {

       public int cx;

       public int cy;

    }

}

新建一个类,命名为CaptureScreen.cs

using System;

using System.Drawing;

namespace CaptureScreen

{

    ///<summary>

    ///CaptureScreen类

    ///</summary>

    public class CaptureScreen

    {

        #region public class Functions

        public static Bitmap GetDesktopImage()

        {

             //屏幕大小

             SIZE size;

              //图像

              IntPtr  hBitmap;

              //User32中的GetDesktopWindow方法

              IntPtr hDC=PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());

              //GDI32中的方法

               Intptr  hMemDC=PlatformInvokeGDI32.CreateCompatibleDC(hDC);

               //宽度

               size.cx =PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CXSCREEN);

                //长度

               size.cy=PlatformInvokeUSE32.GetSystemMetrics(PlatformInvokeUSER32.SM_CYSCREEN);

               //创建图像

               hBitmap =PlatformInvokeGDI32.CreateCompatibleBitmap(hDC,size.cx,size.cy);

               //截屏处理

               if(hBitmap !=IntPtr.Zero)

               {

                     IntPtr hOld=(IntPtr)PlatformInvokeGDI32.SelectObject(hMemDC,hBitmap);

                     PlatformInvokeGDI32.BitBlt(hMemDC,0,0,size.cx,size.cy,hDC,0,0,PlatformInvokeGDI32.SRCCOPY);

                     PlatformInvokeGDI32.SelectObject(hMemDC,hOld);

                     PlatformInvokeGDI32.DeleteDC(hMemDC);

                     PlatformInvokeGDI32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(),hDC);

                    

                     Bitmap  bmp=System.Drawing.Image.FromHbitmap(hBitmap);

                     PlatformInvokeGDI32.DeleteObject(hBitmap);


                     GC.Collect();

                     //返回图像

                     return bmp;

               }

             return null;

    }

    #endregion

}

}


截屏操作:

private void mnuCaptureScreen_Click(object sender,System.EventArgs e)

{

    picScreen.Image=CaptureScreen.GetDesktopImage();

}


private void mnuExit_Click(object sender,System.EventArgs e)

{

    Application.Exit();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值