基于C#实现屏幕取色器的示例详解

 更新时间:2022年12月09日 11:21:55   作者:芝麻粒儿  
这篇文章主要为大家详细介绍了如何利用C#实现屏幕取色器,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以了解一下

实践过程

效果

代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    [DllImport("gdi32.dll")]
    static public extern uint GetPixel(IntPtr hDC, int XPos, int YPos);

    [DllImport("gdi32.dll")]
    static public extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData);

    [DllImport("gdi32.dll")]
    static public extern bool DeleteDC(IntPtr DC);

    static public byte GetRValue(uint color)
    {
        return (byte) color;
    }

    static public byte GetGValue(uint color)
    {
        return ((byte) (((short) (color)) >> 8));
    }

    static public byte GetBValue(uint color)
    {
        return ((byte) ((color) >> 16));
    }

    static public byte GetAValue(uint color)
    {
        return ((byte) ((color) >> 24));
    }

    public Color GetColor(Point screenPoint)
    {
        IntPtr displayDC = CreateDC("DISPLAY", null, null, IntPtr.Zero);
        uint colorref = GetPixel(displayDC, screenPoint.X, screenPoint.Y);
        DeleteDC(displayDC);
        byte Red = GetRValue(colorref);
        byte Green = GetGValue(colorref);
        byte Blue = GetBValue(colorref);
        return Color.FromArgb(Red, Green, Blue);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Point pt = new Point(Control.MousePosition.X, Control.MousePosition.Y);
        Color cl = GetColor(pt);
        panel1.BackColor = cl;
    }
}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.panel1 = new System.Windows.Forms.Panel();
        this.SuspendLayout();
        // 
        // timer1
        // 
        this.timer1.Enabled = true;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // panel1
        // 
        this.panel1.Location = new System.Drawing.Point(12, 12);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(200, 100);
        this.panel1.TabIndex = 0;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(535, 298);
        this.Controls.Add(this.panel1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Panel panel1;
}

到此这篇关于基于C#实现屏幕取色器的示例详解的文章就介绍到这了,更多相关C#屏幕取色器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#中Web.Config加密与解密的方法

    C#中Web.Config加密与解密的方法

    C#中Web.Config加密与解密的方法,需要的朋友可以参考一下
    2013-04-04
  • C#生成Word文档代码示例

    C#生成Word文档代码示例

    这篇文章主要介绍了C#生成Word文档代码示例,本文直接给出代码实例,需要的朋友可以参考下
    2015-06-06
  • Unity实现简单场景分层移动

    Unity实现简单场景分层移动

    这篇文章主要为大家详细介绍了Unity实现简单场景分层移动,分为前景、场景、背景等,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • C#语法之泛型的多种应用

    C#语法之泛型的多种应用

    这篇文章主要介绍了C#语法之泛型的多种应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • C#8.0中的模式匹配

    C#8.0中的模式匹配

    这篇文章介绍了C#8.0中的模式匹配,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • C#实现延时并自动关闭MessageBox的方法

    C#实现延时并自动关闭MessageBox的方法

    这篇文章主要介绍了C#实现延时并自动关闭MessageBox的方法,非常实用的功能,需要的朋友可以参考下
    2014-08-08
  • C#中给Excel添加水印的具体方法

    C#中给Excel添加水印的具体方法

    这篇文章主要介绍了C#中如何给Excel添加水印,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • C#实现简单获取及设置Session类

    C#实现简单获取及设置Session类

    这篇文章主要介绍了C#实现简单获取及设置Session类,涉及C#针对session的设置及获取的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • C# App.config和Web.config加密的实现步骤

    C# App.config和Web.config加密的实现步骤

    本文介绍了如何使用C#对App.config和Web.config文件进行加密,通过使用ConfigurationSection类和SymmetricAlgorithm类,我们可以保护配置文件中的敏感数据,确保只有授权人员可以访问
    2023-08-08
  • C#中的委托、事件学习笔记

    C#中的委托、事件学习笔记

    这篇文章主要介绍了C#中的委托、事件学习笔记,本文讲解了委托delegate、事件的相关知识并给出代码实例,需要的朋友可以参考下
    2015-01-01

最新评论