现在的位置: 首页 > 综合 > 正文

C# 如何在图像上做(矩形/圆)标记

2018年10月17日 ⁄ 综合 ⁄ 共 3379字 ⁄ 字号 评论关闭

手动画圆或椭圆

 

  1. private Point startPoint;  
  2.     private bool beginDragging;  
  3.  
  4.     public Form1()  
  5.     {  
  6.         InitializeComponent();  
  7.         this.DoubleBuffered = true;     
  8.     }  
  9.  
  10.     private void Form1_MouseMove(object sender, MouseEventArgs e)  
  11.     {  
  12.         if (e.Button == MouseButtons.Left && beginDragging)  
  13.         {  
  14.             circle.Size = new Size(circle.Size.Width + e.X - startPoint.X, circle.Size.Height + e.Y - startPoint.Y);  
  15.             startPoint = new Point(e.X, e.Y);  
  16.         }  
  17.         this.Invalidate();  
  18.     }  
  19.  
  20.     private void Form1_MouseDown(object sender, MouseEventArgs e)  
  21.     {  
  22.         if (e.Button == MouseButtons.Left)  
  23.         {  
  24.             startPoint = e.Location;  
  25.             beginDragging = true;  
  26.             this.Capture = true;  
  27.  
  28.             circle = new Circle();  
  29.             circle.Location = e.Location;  
  30.             Circles.Add(circle);  
  31.  
  32.             this.Cursor = System.Windows.Forms.Cursors.Cross;  
  33.         }  
  34.  
  35.     }  
  36.  
  37.     private void Form1_MouseUp(object sender, MouseEventArgs e)  
  38.     {  
  39.         if (e.Button == MouseButtons.Left && beginDragging)  
  40.         {  
  41.             beginDragging = false;  
  42.             this.Capture = false;  
  43.  
  44.             this.Cursor = System.Windows.Forms.Cursors.Default;  
  45.         }  
  46.         this.Invalidate();  
  47.     }  
  48.  
  49.     private void Form1_Paint(object sender, PaintEventArgs e)  
  50.     {  
  51.         Graphics g = e.Graphics;  
  52.         foreach (Circle each in Circles)  
  53.         {  
  54.             g.DrawEllipse(Pens.Black, new Rectangle(each.Location, each.Size));  
  55.         }  
  56.     }  
  57.     private List<Circle> Circles = new List<Circle>();  
  58.     private Circle circle = new Circle();  
  59.  
  60. public class Circle  
  61. {  
  62.     public int Radii;  
  63.     public Size Size;  
  64.     public Point Location;  
  65. }  

自动画圆

  1. //方法1  
  2.         private void button1_Click_1(object sender, EventArgs e)  
  3.         {  
  4.             Image im = new Bitmap(this.panel2.Width, this.panel2.Height);  
  5.             Graphics g = Graphics.FromImage(im);  
  6.             GraphicsPath gp = new GraphicsPath();  
  7.             Rectangle rec = new Rectangle(0, 0, 100, 100);  
  8.             gp.AddEllipse(rec);  
  9.             g.DrawEllipse(new Pen(Color.Red), rec);  
  10.             this.panel2.BackgroundImageLayout = ImageLayout.Stretch;  
  11.             this.panel2.BackgroundImage = im;  
  12.         }  
  13. //方法2  
  14.         private void button3_Click(object sender, EventArgs e)  
  15.         {  
  16.             Graphics gra;  
  17.             Brush bush;  
  18.             gra = this.panel3.CreateGraphics();  
  19.             bush = new SolidBrush(Color.Red); //填充的颜色  
  20.             gra.FillEllipse(bush, 0, 0, 100, 100);//画填充椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50    
  21.         } 

自动画矩形

  1. //方法1  
  2.        private void button2_Click(object sender, EventArgs e)  
  3.        {  
  4.            Image im = new Bitmap(this.panel1.Width,this.panel1.Height);  
  5.            Graphics g = Graphics.FromImage(im);  
  6.            GraphicsPath gp = new GraphicsPath();  
  7.            Rectangle rec = new Rectangle(new Point(0,0),new Size(100,100));  
  8.            gp.AddRectangle(rec);  
  9.            g.DrawRectangle(new Pen(Color.Red), rec);  
  10.            this.panel1.BackgroundImageLayout = ImageLayout.Stretch;  
  11.            this.panel1.BackgroundImage = im;  
  12.        }  
  13. //方法2  
  14.        private void button4_Click(object sender, EventArgs e)  
  15.        {  
  16.            Graphics gra;  
  17.            Brush bush;  
  18.            gra = this.panel4.CreateGraphics();  
  19.            bush = new SolidBrush(Color.Red);  
  20.            gra.DrawRectangle(new Pen(bush,20), 0, 0, 100, 100);  
  21.        } 

抱歉!评论已关闭.