C#获取鼠标光标处屏幕颜色

本文介绍了一个在Windows环境下用C#获取鼠标光标所在位置屏幕颜色的类,该方法未在其他平台进行测试。参考链接提供了获取光标相对屏幕位置的方法。

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

这个是在Windows下获取鼠标处的屏幕颜色的类,在其他平台或者终端环境未做测试。

using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace ColorPicker
{
    /// <summary>
    /// 获取当前光标处颜色,win8下wpf测试成功
    /// </summary>
    public class ColorPickerManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x">鼠标相对于显示器的坐标X</param>
        /// <param name="y">鼠标相对于显示器的坐标Y</param>
        /// <returns></returns>
        public static Color GetColor(int x, int y)
        {
            IntPtr hdc = GetDC(IntPtr.Zero);
            uint pixel = GetPixel(hdc, x, y);
            ReleaseDC(IntPtr.Zero, hdc);
            Color color = Color.FromArgb((int)(pixel & 0x000000FF), (int)(pixel & 

0x0000FF00) >> 8, (int)(pixel & 0x00FF0000) >> 16);

            return color;
        }

     
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值