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

作者:芝麻粒儿 时间:2021-06-26 08:58:05 

实践过程

效果

基于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;
}

来源:https://zhima.blog.csdn.net/article/details/128123604

标签:C#,屏幕,取色器
0
投稿

猜你喜欢

  • Java设计模式之动态代理模式实例分析

    2022-07-07 17:55:07
  • C#简单的向量用法实例教程

    2022-09-27 09:57:29
  • Android读取资源文件的方法

    2022-09-25 22:11:53
  • Java中super关键字介绍以及super()的使用

    2021-09-20 06:31:11
  • Servlet3.0实现文件上传的方法

    2023-08-15 00:52:44
  • Java特性队列和栈的堵塞原理解析

    2023-10-13 14:15:55
  • Spring Boot启动时调用自己的非web逻辑

    2022-02-15 11:40:10
  • 在Winform程序中使用Spire.Pdf实现页面添加印章功能的实现

    2022-05-29 16:57:27
  • 怎么把本地jar包放入本地maven仓库和远程私服仓库

    2023-12-05 20:13:00
  • 深入讲解spring boot中servlet的启动过程与原理

    2022-08-19 00:18:40
  • Java多线程 线程组原理及实例详解

    2022-11-26 02:51:40
  • JDK 7 新特性小结实例代码解析

    2022-04-18 13:03:07
  • JAVA利用递归删除文件代码实例

    2022-12-10 23:59:53
  • IISExpress 配置允许外部访问详细介绍

    2023-08-02 02:18:10
  • Linux下Java开发环境搭建以及第一个HelloWorld

    2023-11-06 01:09:57
  • Java获取字符串编码格式实现思路

    2023-08-14 12:35:38
  • iOS获取AppIcon and LaunchImage's name(app图标和启动图片名字)

    2022-01-11 02:39:14
  • Javaweb中Request获取表单数据的四种方法详解

    2023-10-07 03:07:02
  • Java基础高级综合练习题扑克牌的创建

    2023-09-08 06:56:19
  • C#实现的文件上传下载工具类完整实例【上传文件自动命名】

    2023-03-05 17:39:38
  • asp之家 软件编程 m.aspxhome.com