基于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#实现多种图片格式转换的示例详解

    C#实现多种图片格式转换的示例详解

    这篇文章主要为大家详细介绍了C#如何实现多种图片格式转换,例如转换成图标图像ICO,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • WPF实现XAML转图片的示例详解

    WPF实现XAML转图片的示例详解

    这篇文章主要为大家详细介绍了如何利用WPF实现XAML转图片,文中的示例代码讲解详细,对我们学习或工作有一定帮助,感兴趣的小伙伴可以了解一下
    2022-11-11
  • C#调用QQ_Mail发送邮件实例代码两例

    C#调用QQ_Mail发送邮件实例代码两例

    这篇文章介绍了C#调用QQ_Mail发送邮件的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • 利用C#实现合并Word文档功能

    利用C#实现合并Word文档功能

    合并Word文档可以快速地将多份编辑好的文档合在一起,避免复制粘贴时遗漏内容,以及耗费不必要的时间。本文将分为以下两部分介绍如何通过C#合并Word文档,并附上VB.NET代码供大家参考,希望对大家有所帮助
    2022-12-12
  • C#中Clone一个对象的值到另一个对象案例

    C#中Clone一个对象的值到另一个对象案例

    这篇文章主要介绍了C#中Clone一个对象的值到另一个对象案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • C#中HashTable的定义与使用方法

    C#中HashTable的定义与使用方法

    Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对,所以Hashtable可以支持任何类型的keyvalue键值对
    2012-12-12
  • C#9特性record 类型、模式匹配、init 属性详情

    C#9特性record 类型、模式匹配、init 属性详情

    这篇文章主要介绍了C#的record 类型、模式匹配(Pattern Matching)、属性的 init 访问器三大特性,感兴趣的小伙伴请参考下面文章内容
    2021-09-09
  • C# 中实现ftp 图片上传功能(多快好省)

    C# 中实现ftp 图片上传功能(多快好省)

    这篇文章主要介绍了C# 中实现ftp 图片上传功能(多快好省),需要的朋友可以参考下
    2017-06-06
  • C#中AS和IS关键字的用法

    C#中AS和IS关键字的用法

    这篇文章主要介绍了C#中AS和IS关键字的用法的相关资料,需要的朋友可以参考下
    2016-03-03
  • C#使用Aspose.Cells控件读取Excel

    C#使用Aspose.Cells控件读取Excel

    本文介绍Aspose.Cells基础的用法,供大家参考。
    2016-03-03

最新评论