效果图
获取winfrom控件上图片的颜色值的核心代码
public partial class Form2 : Form { public Form2() { InitializeComponent(); } int x, y; private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { // 创建Bitmap对象加载图片 Bitmap image = pictureBox1.Image as Bitmap;// new Bitmap(imagePath); //int tempx = image.Width / pictureBox1.Width; //int tempy = image.Height / pictureBox1.Height; x = e.X; y = e.Y; // 获取像素点的颜色 Color pixelColor = image.GetPixel(x, y); // 可以通过pixelColor的R、G、B、A属性获取分别对应的红、绿、蓝、透明度值 int red = pixelColor.R; int green = pixelColor.G; int blue = pixelColor.B; int alpha = pixelColor.A; label2.Text = "R:" + red + " G:" + green + " B:" + blue + " alpha:" + alpha + " x" + x + " y" + y; panel1.BackColor = Color.FromArgb(red, green, blue); } private void pictureBox2_MouseMove(object sender, MouseEventArgs e) { //lqwvje 2024-05-17 //为了用户更好的体验,我们可以这样, //if (Control.MouseButtons == MouseButtons.Left) //当鼠标按下左键时获取颜色值 //当鼠标左键松开时,确认所获取的颜色值 把下面的点击事件改成 MouseUp 事件 // 创建Bitmap对象加载图片 Bitmap image = pictureBox2.Image as Bitmap;// new Bitmap(imagePath); int tempx = image.Width / pictureBox2.Width; int tempy = image.Height / pictureBox2.Height; x = e.X * tempx;//成比例的缩放 y = e.Y * tempy; // 获取像素点的颜色 Color pixelColor = image.GetPixel(x, y); // 可以通过pixelColor的R、G、B、A属性获取分别对应的红、绿、蓝、透明度值 int red = pixelColor.R; int green = pixelColor.G; int blue = pixelColor.B; int alpha = pixelColor.A; label2.Text = "R:" + red + " G:" + green + " B:" + blue + " alpha:" + alpha + " x" + x + " y" + y; panel1.BackColor = Color.FromArgb(red, green, blue); } private void pictureBox1_Click(object sender, EventArgs e) { Bitmap image = pictureBox1.Image as Bitmap;// new Bitmap(imagePath); // 获取像素点的颜色 Color pixelColor = image.GetPixel(x, y); // 可以通过pixelColor的R、G、B、A属性获取分别对应的红、绿、蓝、透明度值 int red = pixelColor.R; int green = pixelColor.G; int blue = pixelColor.B; int alpha = pixelColor.A; label1.Text = "左击确认选中的颜色 " + "R:" + red + " G:" + green + " B:" + blue + " alpha:" + alpha + " x" + x + " y" + y; panel2.BackColor = Color.FromArgb(red, green, blue); } private void pictureBox2_Click(object sender, EventArgs e) { Bitmap image = pictureBox2.Image as Bitmap;// new Bitmap(imagePath); // 获取像素点的颜色 Color pixelColor = image.GetPixel(x, y); // 可以通过pixelColor的R、G、B、A属性获取分别对应的红、绿、蓝、透明度值 int red = pixelColor.R; int green = pixelColor.G; int blue = pixelColor.B; int alpha = pixelColor.A; label1.Text = "左击确认选中的颜色 " + "R:" + red + " G:" + green + " B:" + blue + " alpha:" + alpha + " x" + x + " y" + y; panel2.BackColor = Color.FromArgb(red, green, blue); } }
获取屏幕上某区域的颜色值核心代码(注意电脑分辨率的比例)
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 timer1_Tick(object sender, EventArgs e) { Point pt = new Point(Control.MousePosition.X, Control.MousePosition.Y); Color cl = GetColor(pt); panel1.BackColor = cl; this.Text = cl.R + " " + cl.G + " " + cl.B; } private void Form1_Load(object sender, EventArgs e) { timer1.Start(); } }
相关素材和源码下载地址 https://pan.baidu.com/s/1PTt1r7mCywGGac_pC0Bysw?pwd=d7tk 提取码: d7tk
本文来自 www.luofenming.com