zoukankan      html  css  js  c++  java
  • .NET获取当前鼠标所在位置像素的颜色

      本文主要是获得鼠标当前所在位置像素的颜色。说到这里,大家可能还记得PhotoShop中吸管的功能,就是在图片的某一位置点击一下,就能获得那个位置像素的颜色。此程序和吸管的功能相似,不妨先来看下运行效果图:
     
     
     
      其代码如下所示:
     
    class CallWin32GDIAPI
    {
            private const int SM_CXSCREEN = 0x00000000;//屏幕的横坐标
            private const int SM_CYSCREEN = 0x00000001;//纵坐标
            private const int SRCCOPY = 0x00CC0020;//光栅操作一参数
    
            [DllImport("GDI32.dll", EntryPoint = "DeleteDC")]
            public static extern IntPtr DeleteDC(IntPtr hdc);
    
            [DllImport("GDI32.dll", EntryPoint = "DeleteObject")]
            public static extern IntPtr DeleteObject(IntPtr hObject);
    
            [DllImport("GDI32.dll", EntryPoint = "BitBlt")]
            public static extern IntPtr BitBlt(IntPtr hdcDest,int nXDest,int nYDest,
                                               int nWidth,int nHight,IntPtr dhcSRC,
                                               int nXSRC,int nYSRC,int dwRop);
    
            [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 hgdiobj);
    
            [DllImport("User32.dll", EntryPoint = "GetDesktopWindow")]
            public static extern IntPtr GetDesktopWindow();
    
            [DllImport("User32.dll", EntryPoint = "GetDC")]
            public static extern IntPtr GetDC(IntPtr hWnd);
    
            [DllImport("User32.dll", EntryPoint = "GetSystemMetrics")]
            public static extern IntPtr GetSystemMetrics(int nIndex);
    
            [DllImport("User32.dll", EntryPoint = "ReleaseDC")]
            public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hdc);
    
            /// <summary>
            /// 返回像素的颜色
            /// </summary>
            /// <returns></returns>
            internal static Bitmap GetDesktop()
            {
                //屏幕分辨率宽与高,以像素为单位
                int Xscreen;
                int Yscreen;
                IntPtr hBmp;
                //检索指定窗口句柄
                IntPtr hdcScreen = GetDC(GetDesktopWindow());
                //创建一个与设备兼容的内在设备上下文环境
                IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen);
                //得到屏幕分辨率宽与高,以像素为单位
                Xscreen = (int)GetSystemMetrics(SM_CXSCREEN);
                Yscreen = (int)GetSystemMetrics(SM_CYSCREEN);
                //创建与设备相兼容的位图
                hBmp = CreateCompatibleBitmap(hdcScreen, Xscreen, Yscreen);
    
                if (hBmp != IntPtr.Zero)
                {
                    //将位图对象指定到相关的设备上下文环境中
                    IntPtr hOldBmp = (IntPtr)SelectObject(hdcCompatible, hBmp);
                    //对位图的像素进行转换
                    BitBlt(hdcCompatible, 0, 0, Xscreen, Yscreen, hdcScreen, 0, 0, SRCCOPY);
                    //SRCCOPY:将源矩形区域直接拷贝到目标矩形区域
                    SelectObject(hdcCompatible, hOldBmp);
                    //释放系统资源
                    DeleteDC(hdcCompatible);
                    ReleaseDC(GetDesktopWindow(), hdcScreen);
                    //根据返回的句柄创建位图对象
                    Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);
                    DeleteObject(hBmp);
                    //强制进行垃圾回收
                    GC.Collect();
    
                    return bmp;
                }
                return null;
            }
        }


      最后,希望转载的朋友能够尊重作者的劳动成果,加上转载地址:http://www.cnblogs.com/hanyonglu/archive/2011/04/12/2014248.html  谢谢。
     

      完毕。^_^ 

  • 相关阅读:
    【centos6.5 安装 node.js + npm】
    【钉钉PC】PC端钉钉清除缓存
    【laravel5.4】中jquery的post Ajax提交
    python 设计模式之中介者模式
    python 设计模式之备忘录模式
    python 设计模式之观察者模式
    python 设计模式之策略模式
    23种设计模式有哪些,不带定义,不带例子
    python 设计模式之模板方法模式
    python 设计模式之访问者模式
  • 原文地址:https://www.cnblogs.com/hanyonglu/p/2014248.html
Copyright © 2011-2022 走看看