在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 画线的新想法

画线的新想法

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.06M
  • 下载次数:20
  • 浏览次数:228
  • 发布时间:2015-09-15
  • 实例类别:C#语言基础
  • 发 布 人:huangsd
  • 文件格式:.zip
  • 所需积分:2
 相关标签: C# 画线

实例介绍

【实例简介】

【实例截图】

【核心代码】

/*
 * *******************************
 * Line Editor by Marco Bellini  *
 *								 *
 * Please send me feedbacks! :D  *
 * butch.er@tin.it               *
 *								 *
 * ...and share Knowledge!		 *
 *								 *
 * *******************************
*/


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;



namespace LineEditor
{
	
	public class Form1 : System.Windows.Forms.Form
	{
		
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.PictureBox image;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem15;
		private System.Windows.Forms.SaveFileDialog sfd;
		private System.Windows.Forms.MenuItem menuItem17;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.OpenFileDialog ofd;
		
		
		private System.Windows.Forms.ContextMenu linecmenu;
		private System.Windows.Forms.MenuItem LWidth;
		private System.Windows.Forms.MenuItem Delete;
		
		Bitmap bmpBack = null;
		private bool isSelected = false;
		private int _X, _Y;
		private ArrayList Lines = null;
		

		private struct Line
		{
			public MarkControl mark1;
			public MarkControl mark2;
			public int Width;
		}


		public Form1()
		{
			InitializeComponent();

			Lines = new ArrayList();
			bmpBack = new Bitmap(image.Width,image.Height);
			Graphics.FromImage(bmpBack).Clear(Color.White);
			image.Image = (Bitmap)bmpBack.Clone();
            
			this.SetStyle(ControlStyles.DoubleBuffer, true);
			this.SetStyle(ControlStyles.UserPaint, true);
			this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);		
		}

	
			
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}

			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.image = new System.Windows.Forms.PictureBox();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem17 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem15 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.ofd = new System.Windows.Forms.OpenFileDialog();
			this.sfd = new System.Windows.Forms.SaveFileDialog();
			this.button1 = new System.Windows.Forms.Button();
			this.linecmenu = new System.Windows.Forms.ContextMenu();
			this.LWidth = new System.Windows.Forms.MenuItem();
			this.Delete = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// image
			// 
			this.image.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.image.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.image.Location = new System.Drawing.Point(16, 16);
			this.image.Name = "image";
			this.image.Size = new System.Drawing.Size(400, 400);
			this.image.TabIndex = 3;
			this.image.TabStop = false;
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem17,
																					  this.menuItem2,
																					  this.menuItem15,
																					  this.menuItem4,
																					  this.menuItem3});
			this.menuItem1.Text = "&File";
			// 
			// menuItem17
			// 
			this.menuItem17.Index = 0;
			this.menuItem17.Text = "&New";
			this.menuItem17.Click  = new System.EventHandler(this.menuItem17_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.Text = "&Load background";
			this.menuItem2.Click  = new System.EventHandler(this.menuItem2_Click);
			// 
			// menuItem15
			// 
			this.menuItem15.Index = 2;
			this.menuItem15.Text = "&Save";
			this.menuItem15.Click  = new System.EventHandler(this.menuItem15_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 3;
			this.menuItem4.Text = "-";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 4;
			this.menuItem3.Text = "&Exit";
			this.menuItem3.Click  = new System.EventHandler(this.menuItem3_Click);
			// 
			// ofd
			// 
			this.ofd.Filter = "Image Files|*.*";
			this.ofd.FileOk  = new System.ComponentModel.CancelEventHandler(this.ofd_FileOk);
			// 
			// sfd
			// 
			this.sfd.DefaultExt = "jpg";
			this.sfd.Filter = "JPEG Image Files (*.jpg)|*.jpg";
			this.sfd.Title = "Save Cover Page";
			this.sfd.FileOk  = new System.ComponentModel.CancelEventHandler(this.sfd_FileOk);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(424, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(112, 40);
			this.button1.TabIndex = 11;
			this.button1.Text = "Draw Line";
			this.button1.Click  = new System.EventHandler(this.button1_Click);
			// 
			// linecmenu
			// 
			this.linecmenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.LWidth,
																					  this.Delete});
			// 
			// LWidth
			// 
			this.LWidth.Index = 0;
			this.LWidth.Text = "Width";
			this.LWidth.Click  = new System.EventHandler(this.Width_Click);
			// 
			// Delete
			// 
			this.Delete.Index = 1;
			this.Delete.Text = "Delete";
			this.Delete.Click  = new System.EventHandler(this.Delete_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.AutoScroll = true;
			this.BackColor = System.Drawing.SystemColors.Control;
			this.ClientSize = new System.Drawing.Size(552, 453);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.image);
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Line Editor";
			this.ResumeLayout(false);

		}
		#endregion

		
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}



		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			ofd.AddExtension = true;
			ofd.CheckFileExists = true;
			ofd.Title = "Select an image file";
			ofd.ReadOnlyChecked = true;
			ofd.InitialDirectory = Application.StartupPath;
			ofd.Multiselect = false;
			ofd.Filter = "JPEG Files (*.jpg)|*.jpg|Image Files(*.*)|*.*";
			ofd.ShowDialog();

		}
		//Loads bg
		private void ofd_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
			try
			{
				bmpBack = new Bitmap(ofd.FileName);

				image.Image = (Bitmap)bmpBack.Clone();
				image.SendToBack();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}

			Redraw();
		}

		
		
		private void menuItem15_Click(object sender, System.EventArgs e)
		{
			sfd.InitialDirectory = Application.StartupPath;
			sfd.ShowDialog();
		}
		
		//Saves file
		private void sfd_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
			try
			{
				image.Image.Save(sfd.FileName,ImageFormat.Jpeg);
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message, "Error");
			}
		}
		
		//Exit
		private void menuItem3_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		//New
		private void menuItem17_Click(object sender, System.EventArgs e)
		{
			image.Controls.Clear();
			bmpBack = new Bitmap(image.Width,image.Height);
			image.Image = (Bitmap)bmpBack.Clone();
			
			Lines.Clear();
		}

		
		private void button1_Click(object sender, System.EventArgs e)
		{
			try
			{
				//Adds red marks that are the beginning/end of the line
				MarkControl mark1 = new MarkControl();
				mark1.Location = new Point(50,50);
				mark1.ContextMenu = linecmenu;
				image.Controls.Add(mark1);

				MarkControl mark2 = new MarkControl();
				mark2.Location = new Point(100,100);
				mark2.ContextMenu = linecmenu;
				image.Controls.Add(mark2);

				//Line Struct contains the information for a single line
				Line line = new Line();
				line.mark1 = mark1;
				line.mark2 = mark2;
				line.Width = 1;


				//Events for moving marks
				mark1.MouseUp  = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseUp);
				mark1.MouseDown  = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseDown);
				mark1.MouseMove  = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseMove);

				mark2.MouseUp  = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseUp);
				mark2.MouseDown  = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseDown);
				mark2.MouseMove  = new System.Windows.Forms.MouseEventHandler(this.Mark_MouseMove);

				//Adds Line object to an arraylist
				Lines.Add(line);
				Redraw();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		
		//Simply draws a line
		private void DrawLine(Line line)
		{
			Graphics g = null;
					
			g = Graphics.FromImage(image.Image);
			g.DrawLine(new Pen(Color.Black,(float)line.Width),line.mark1.Center.X,line.mark1.Center.Y,line.mark2.Center.X,line.mark2.Center.Y);		
			g.Dispose();
		}

		//Redraws all the lines and a part of the background
		private void Redraw(Line line, Point p)
		{
			
           
			Graphics.FromImage(image.Image).DrawImage(bmpBack,0,0,image.Image.Width,
				image.Image.Height);

			foreach(Line l in Lines)
			{
				DrawLine(l);
			}

			Region r = getRegionByLine(line,p);
			image.Invalidate(r);
			image.Update();
		}

		
		//Redraws all the lines and the background too
		private void Redraw()
		{
			if(bmpBack!=null)
				image.Image = (Bitmap)bmpBack.Clone();
			else 
			{
				image.Image = new Bitmap(image.Width,image.Height);
				Graphics.FromImage(image.Image).Clear(Color.Transparent);
			}
            
			foreach(Line l in Lines)
			{
				DrawLine(l);
			}
			image.Refresh();	
		}

		private void Mark_MouseDown(object sender, MouseEventArgs e)
		{
			this.SuspendLayout();
			isSelected = true;
			_X = e.X;
			_Y = e.Y;
		}

		private void Mark_MouseMove(object sender, MouseEventArgs e)
		{
			if(isSelected)
			{			
				MarkControl mc1 = (MarkControl)sender;
				Line l = getLineByMark(mc1);			
	
				Point p = new Point(e.X - _X   mc1.Left, e.Y - _Y   mc1.Top);
				
				mc1.Location = p;
				
				Redraw(l,p);
			}	
		}

		private void Mark_MouseUp(object sender, MouseEventArgs e)
		{
			isSelected = false; 
			ResumeLayout();
			Redraw();
		}

		//Retrieves a mark having the other one
		private MarkControl getOtherMark(MarkControl m)
		{
			foreach(Line l in Lines)
			{
				if(l.mark1 == m)
					return l.mark2;
				
				if(l.mark2 == m)
					return l.mark1;
			}//Never happens :D
			throw new Exception("No relative mark found");
		}

		//Retrieves a Line object having a mark
		private Line getLineByMark(MarkControl m)
		{
			foreach(Line l in Lines)
			{
				if(l.mark1 == m || l.mark2 == m)
					return l;	
			}//Never happens :D
			throw new Exception("No line found");
		}

		//Shows Dialog to change line's width
		private void Width_Click(object sender, System.EventArgs e)
		{		
			Line l;
			wdialog wd = new wdialog(getLineByMark((MarkControl)linecmenu.SourceControl).Width);
			DialogResult res = wd.ShowDialog();
			if(res == DialogResult.OK) 
			{
				l = getLineByMark((MarkControl)linecmenu.SourceControl);
				Lines.Remove(l);
				l.Width = wd.LineWidth;
				Lines.Add(l);
				Redraw();
			}		
		}
		//Deletes line
		private void Delete_Click(object sender, System.EventArgs e)
		{
			Line l = getLineByMark(((MarkControl)linecmenu.SourceControl));
			Lines.Remove(l);
			l.mark1.Dispose();
			l.mark2.Dispose();
			linecmenu.SourceControl.Dispose();
			Redraw();
		}

		//Returns the region to update
		private Region getRegionByLine(Line l, Point p)
		{
			GraphicsPath gp = new GraphicsPath();
			gp.AddPolygon(new Point[]{l.mark1.Center,l.mark2.Center,p,l.mark1.Center});
			
			RectangleF rf = gp.GetBounds();
			gp.Dispose();
			
			rf.Inflate(100f,100f);
			
			return new Region(rf);
		}

		
	}

}

标签: C# 画线

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警