实例介绍
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace 图像识别 { public partial class Form1 : Form { private int num_old_x; private int num_old_y; private int num_new_x; private int num_new_y; private int x; private int y; private bool bool_MouseDown = false; private bool bool_beginCutPic = false; private bool bool_beginIdentify = false; Bitmap OldBT; Bitmap NewBT; Bitmap Source; //用于窗体拖动 private bool bool_begin_move = false; private int num_mouseDown_X = 0; private int num_mouseDown_Y = 0; public Form1() { InitializeComponent(); } // //图像区域选择 // private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { pictureBox1.Refresh(); if (pictureBox1.BackgroundImage != null && bool_beginCutPic==true) { bool_MouseDown = true; num_old_x = MousePosition.X; num_old_y = MousePosition.Y; } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (bool_MouseDown == true) { num_new_x = MousePosition.X; num_new_y = MousePosition.Y; x = num_old_x - this.Location.X - pictureBox1.Location.X; y = num_old_y - this.Location.Y - pictureBox1.Location.Y; Graphics g = pictureBox1.CreateGraphics(); g.DrawRectangle(Pens.White, x, y, num_new_x - num_old_x, num_new_y - num_old_y); pictureBox1.Refresh(); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (bool_MouseDown == true) { bool_MouseDown = false; Graphics g = pictureBox1.CreateGraphics(); g.DrawRectangle(Pens.White, x, y, num_new_x - num_old_x, num_new_y - num_old_y); Bitmap bt = new Bitmap(num_new_x - num_old_x, num_new_y - num_old_y); for (int i = x; i < x num_new_x - num_old_x; i ) { for (int j = y; j < y num_new_y - num_old_y; j ) { bt.SetPixel(i - x, j - y, OldBT.GetPixel(i, j)); } } pictureBox2.BackgroundImage = bt; NewBT = bt; Source = bt; 开始识别ToolStripMenuItem.Enabled = true; lab_info.Text = "请开始进行开始提取操作."; } } // //最小化和退出程序 // private void labelX1_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } // //窗体拖动 // private void labelX2_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { bool_begin_move = true; num_mouseDown_X = e.X; num_mouseDown_Y = e.Y; } } private void labelX2_MouseMove(object sender, MouseEventArgs e) { if (bool_begin_move) { Point temp = new Point(0, 0); temp.X = this.Location.X (e.X - num_mouseDown_X); temp.Y = this.Location.Y (e.Y - num_mouseDown_Y); this.Location = temp; } } private void labelX2_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) bool_begin_move = false; } // //打开新图片 // private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "jpg,bmp files (*.jpg,*jpeg,*.bmp)|*.jpg;*.bmp;*.jpeg"; ofd.FilterIndex = 1; if (ofd.ShowDialog() == DialogResult.OK) { OldBT = new Bitmap(ofd.FileName); pictureBox1.BackgroundImage = OldBT; 开始截图ToolStripMenuItem.Enabled = true; lab_info.Text = "请进行开始截图或者对图像进行黑白变换操作."; if (黑贝变换ToolStripMenuItem.Enabled == false) { 黑贝变换ToolStripMenuItem.Enabled = true; } this.DisplayScrollBars(); this.SetScrollBarValues(); } } // //pictureBox3的图片显示格式 // private void strechToolStripMenuItem_Click(object sender, EventArgs e) { pictureBox3.BackgroundImageLayout = ImageLayout.Stretch; } private void zoomToolStripMenuItem_Click(object sender, EventArgs e) { pictureBox3.BackgroundImageLayout = ImageLayout.Zoom; } private void centerToolStripMenuItem_Click(object sender, EventArgs e) { pictureBox3.BackgroundImageLayout = ImageLayout.Center; } private void noneToolStripMenuItem_Click(object sender, EventArgs e) { pictureBox3.BackgroundImageLayout = ImageLayout.None; } // //开始截图和识别 // private void 开始截图ToolStripMenuItem_Click(object sender, EventArgs e) { bool_beginCutPic = true; lab_info.Text = "请在源图片中进行区域截图操作."; 打开ToolStripMenuItem.Enabled = false; } private void 开始识别ToolStripMenuItem_Click(object sender, EventArgs e) { bool_beginCutPic = false; 开始截图ToolStripMenuItem.Enabled = false; 开始识别ToolStripMenuItem.Enabled = false; bool_beginIdentify = true; this.Cursor = Cursors.Cross; lab_info.Text = "请在截图窗口中点击文字颜色集中的位置."; } private void pictureBox2_Click(object sender, EventArgs e) { if (bool_beginIdentify == true) { int x = MousePosition.X - this.Location.X - pictureBox2.Location.X; int y = MousePosition.Y - this.Location.Y - pictureBox2.Location.Y; int num_color = Source.GetPixel(x, y).G; for (int i = 0; i < NewBT.Width; i ) { for (int j = 0; j < NewBT.Height; j ) { if (Source.GetPixel(i, j).G - num_color > -(int)numUpDown_color.Value && Source.GetPixel(i, j).G - num_color < (int)numUpDown_color.Value) { NewBT.SetPixel(i, j, Color.Black); } // NewBT.SetPixel(i, j, Color.White); //else // NewBT.SetPixel(i, j, Color.Black); else NewBT.SetPixel(i, j, Color.White); } } pictureBox3.BackgroundImage = NewBT; pictureBox3.Refresh(); 保存识别ToolStripMenuItem.Enabled = true; lab_info.Text = "现在可对已识别图片进行保存."; } } // //黑白变换 // private void 黑贝变换ToolStripMenuItem_Click(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; Thread t = new Thread(new ThreadStart(ChangeColor)); t.Start(); } private void ChangeColor() { int num_green; for (int i = 0; i < OldBT.Width; i ) { for (int j = 0; j < OldBT.Height; j ) { num_green = (OldBT.GetPixel(i, j).B OldBT.GetPixel(i, j).G OldBT.GetPixel(i, j).R) / 3; OldBT.SetPixel(i, j, Color.FromArgb(num_green, num_green, num_green)); } pictureBox1.BackgroundImage = OldBT; pictureBox1.Refresh(); } 黑贝变换ToolStripMenuItem.Enabled = false; } // //保存识别结果 // private void 保存识别ToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "jpg file (*.jpg)|*.jpg"; if (sfd.ShowDialog() == DialogResult.OK) { pictureBox3.BackgroundImage.Save(sfd.FileName); lab_info.Text = "图像文字提取系统"; this.Cursor = Cursors.Default; pictureBox2.BackgroundImage = null; pictureBox3.BackgroundImage = null; pictureBox1.BackgroundImage = null; 打开ToolStripMenuItem.Enabled = true; } } // //放弃所有操作 // private void 放弃ToolStripMenuItem_Click(object sender, EventArgs e) { lab_info.Text = "图像文字提取系统"; this.Cursor = Cursors.Default; pictureBox2.BackgroundImage = null; pictureBox3.BackgroundImage = null; pictureBox1.BackgroundImage = null; 打开ToolStripMenuItem.Enabled = true; } // //pictureBox实现滚动 // private void Form1_Resize(Object sender, EventArgs e) { // If the PictureBox has an image, see if it needs // scrollbars and refresh the image. if (pictureBox1.Image != null) { this.DisplayScrollBars(); this.SetScrollBarValues(); this.Refresh(); } } public void DisplayScrollBars() { // If the image is wider than the PictureBox, show the HScrollBar. if (pictureBox1.Width > pictureBox1.BackgroundImage.Width - this.vScrollBar1.Width) { hScrollBar1.Visible = false; } else { hScrollBar1.Visible = true; } // If the image is taller than the PictureBox, show the VScrollBar. if (pictureBox1.Height > pictureBox1.BackgroundImage.Height - this.hScrollBar1.Height) { vScrollBar1.Visible = false; } else { vScrollBar1.Visible = true; } } private void HandleScroll(Object sender, ScrollEventArgs se) { /* Create a graphics object and draw a portion of the image in the PictureBox. */ Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(pictureBox1.BackgroundImage, new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width, pictureBox1.Bottom - hScrollBar1.Height), new Rectangle(hScrollBar1.Value, vScrollBar1.Value, pictureBox1.Right - vScrollBar1.Width, pictureBox1.Bottom - hScrollBar1.Height), GraphicsUnit.Pixel); pictureBox1.Update(); } public void SetScrollBarValues() { // Set the Maximum, Minimum, LargeChange and SmallChange properties. this.vScrollBar1.Minimum = 0; this.hScrollBar1.Minimum = 0; // If the offset does not make the Maximum less than zero, set its value. if ((this.pictureBox1.BackgroundImage.Size.Width - pictureBox1.ClientSize.Width) > 0) { this.hScrollBar1.Maximum = this.pictureBox1.BackgroundImage.Size.Width - pictureBox1.ClientSize.Width; } // If the VScrollBar is visible, adjust the Maximum of the // HSCrollBar to account for the width of the VScrollBar. if (this.vScrollBar1.Visible) { this.hScrollBar1.Maximum = this.vScrollBar1.Width; } this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10; this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20; // Adjust the Maximum value to make the raw Maximum value // attainable by user interaction. this.hScrollBar1.Maximum = this.hScrollBar1.LargeChange; // If the offset does not make the Maximum less than zero, set its value. if ((this.pictureBox1.BackgroundImage.Size.Height - pictureBox1.ClientSize.Height) > 0) { this.vScrollBar1.Maximum = this.pictureBox1.BackgroundImage.Size.Height - pictureBox1.ClientSize.Height; } // If the HScrollBar is visible, adjust the Maximum of the // VSCrollBar to account for the width of the HScrollBar. if (this.hScrollBar1.Visible) { this.vScrollBar1.Maximum = this.hScrollBar1.Height; } this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10; this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20; // Adjust the Maximum value to make the raw Maximum value // attainable by user interaction. this.vScrollBar1.Maximum = this.vScrollBar1.LargeChange; } } }
标签: c
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论