C#图像热区识别
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace ImageRectInfo{ public partial class Form1 : Form {//- -!、、尼玛 我感觉所谓的热区 在我这里其实就是再做一份图像出来 public Form1() { InitializeComponent(); this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; this.pictureBox1.BorderStyle = BorderStyle.Fixed3D; this.pictureBox1.Location = new Point(0, 0); } Bitmap srcImg; //原始图片 Bitmap infoImg; //参考图片 Label lbInfo; //显示信息 private void Form1_Load(object sender, EventArgs e) { srcImg = new Bitmap("srcImage.jpg"); //加载图像 infoImg = new Bitmap("infoImage.bmp"); pictureBox1.Image = srcImg; //显示原图 - -!、尼玛感觉srcImage创建出来是多余的 this.Width = pictureBox1.Width (this.Bounds.Width - this.ClientSize.Width); this.Height = pictureBox1.Height (this.Bounds.Height - this.ClientSize.Height); //设置label lbInfo = new Label(); this.Controls.Add(lbInfo); lbInfo.Parent = pictureBox1; lbInfo.BackColor = Color.FromArgb(150, 0, 0, 0); lbInfo.ForeColor = Color.White; lbInfo.TextAlign = ContentAlignment.MiddleCenter; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { Color clr = infoImg.GetPixel(e.X, e.Y); //获取在参考图对应坐标像素点的颜色信息 if (clr.ToArgb() != Color.Black.ToArgb()) { pictureBox1.Cursor = Cursors.Hand; //设置手形 if (clr.ToArgb() == Color.FromArgb(255, 0xFF, 0, 0).ToArgb()) lbInfo.Text = "眼睛"; //对颜色进行判断 else if (clr.ToArgb() == Color.FromArgb(255, 0, 0xFF, 0).ToArgb()) lbInfo.Text = "电脑"; else if (clr.ToArgb() == Color.FromArgb(255, 0, 0, 0xFF).ToArgb()) lbInfo.Text = "鼠标"; else if (clr.ToArgb() == Color.FromArgb(255, 0xFF, 0xFF, 0).ToArgb()) lbInfo.Text = "手机"; else if (clr.ToArgb() == Color.FromArgb(255, 0xFF, 0, 0xFF).ToArgb()) lbInfo.Text = "咖啡杯"; else if (clr.ToArgb() == Color.FromArgb(255, 0, 0xFF, 0xFF).ToArgb()) lbInfo.Text = "徽章"; else if (clr.ToArgb() == Color.FromArgb(255, 0x55, 0, 0).ToArgb()) lbInfo.Text = "钢笔"; else if (clr.ToArgb() == Color.FromArgb(255, 0, 0x55, 0).ToArgb()) lbInfo.Text = "书"; else if (clr.ToArgb() == Color.FromArgb(255, 0, 0, 0x55).ToArgb()) lbInfo.Text = "耳机"; else if (clr.ToArgb() == Color.FromArgb(255, 0x55, 0x55, 0).ToArgb()) lbInfo.Text = "手表"; else if (clr.ToArgb() == Color.FromArgb(255, 0x55, 0, 0x55).ToArgb()) lbInfo.Text = "扑克牌"; } else { pictureBox1.Cursor = Cursors.Arrow; lbInfo.Text = "没有发现东西"; } //设置label位置 if (e.X 20 lbInfo.Width >= pictureBox1.Width) lbInfo.Left = e.X - 20 - lbInfo.Width; else lbInfo.Left = e.X 20; if (e.Y 20 lbInfo.Height >= pictureBox1.Height) lbInfo.Top = e.Y - 20 - lbInfo.Height; else lbInfo.Top = e.Y 20; } }}
评论