打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用C#提取鼠标所指的像素

 用 C# 提取鼠标所指的像素

此出是调研WinAPI,具体代码如下

1、调用API的类
程序代码


using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

namespace Desktop
{
    public class Win32APICall
    {
        [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 bool BitBlt(IntPtr hdcDest,int nXDest,
            int nYDest,int nWidth,int nHeight,IntPtr hdcSrc,
            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 hgdiobjBmp);

        [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 int GetSystemMetrics(int nIndex);

        [DllImport("user32.dll",EntryPoint="ReleaseDC")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);

        public static Bitmap GetDesktop()
        {
            int screenX;
            int screenY;
            IntPtr hBmp;
            IntPtr  hdcScreen = GetDC(GetDesktopWindow());
            IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen);

            screenX = GetSystemMetrics(0);
            screenY = GetSystemMetrics(1);
            hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY);

            if (hBmp!=IntPtr.Zero)
            {
                IntPtr hOldBmp = (IntPtr) SelectObject(hdcCompatible, hBmp);
                BitBlt(hdcCompatible, 0, 0,screenX,screenY, hdcScreen, 0, 0,13369376);
            
                SelectObject(hdcCompatible, hOldBmp);
                DeleteDC(hdcCompatible);
                ReleaseDC(GetDesktopWindow(), hdcScreen);
            
                Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp); 
            
                DeleteObject(hBmp);
                GC.Collect();
            
                return bmp;
            }
        
            return null;
        }
    }
}
 



2、声明
在需要的应用程序声明
private Bitmap myBitmap;

3、抓取

myBitmap = Win32APICall.GetDesktop();

4、结束

Color myColor = myBitmap.GetPixel(MousePosition.X,MousePosition.Y);
            label1.BackColor = myColor;
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Screen Capture and Save as an Image
常用Win32API 封装 Win32api for .net
c#
C# 开发圆角控件(窗体)
c#隐藏显示任务栏,要利用API
C# 禁用控制台应用程序关闭按钮
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服