www.pudn.com > chenmo.rar > FrmMain.cs


using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.Drawing .Drawing2D ; 
 
namespace chenmo 
{ 
	///  
	/// Form1 的摘要说明。 
	///  
	public class FrmMain : System.Windows.Forms.Form 
	{ 
		public System.Drawing .Point pst; 
		public System.Drawing .Point pen; 
		private int MyDrawType; 
		public int drawingline; 
		public int drawingyuan; 
		public bool n=false; 
		public Color r=Color.Black ; 
		public float fx; 
		public float fy; 
		public bool a=false; 
		public string  s; 
 
		public System.Windows.Forms.PictureBox pictureBox1; 
		private System.Windows.Forms.MainMenu mainMenu1; 
		private System.Windows.Forms.MenuItem Mnufile; 
		private System.Windows.Forms.MenuItem Open; 
		private System.Windows.Forms.MenuItem Save; 
		private System.Windows.Forms.MenuItem MnuEdit; 
		private System.Windows.Forms.MenuItem EditPoint; 
		private System.Windows.Forms.MenuItem EditLine; 
		private System.Windows.Forms.MenuItem EditEllipse; 
		private System.Windows.Forms.MenuItem MnuSelect; 
		private System.Windows.Forms.MenuItem MnuView; 
		private System.Windows.Forms.MenuItem Zoomout; 
		private System.Windows.Forms.MenuItem ZoomIn; 
		private System.Windows.Forms.MenuItem MnuHelp; 
		private System.Windows.Forms.SaveFileDialog saveFileDialog1; 
		private System.Windows.Forms.MenuItem staredit; 
		private System.Windows.Forms.MenuItem EditRectangle; 
		private System.Windows.Forms.ContextMenu contextMenu1; 
		private System.Windows.Forms.MenuItem PenColor; 
		private System.Windows.Forms.MenuItem YNfill; 
		private System.Windows.Forms.MenuItem V3; 
		private System.Windows.Forms.MenuItem V4; 
		private System.Windows.Forms.MenuItem V5; 
		private System.Windows.Forms.PictureBox pictureBox2; 
		private AxPicClip.AxPictureClip axPictureClip1; 
		private System.Windows.Forms.MenuItem menuItem1; 
		private System.Windows.Forms.MenuItem menuItem2; 
		private System.Windows.Forms.MenuItem MicHelp; 
		private System.Windows.Forms.MenuItem SelectR; 
		///  
		/// 必需的设计器变量。 
		///  
		private System.ComponentModel.Container components = null; 
 
		public FrmMain() 
		{ 
			// 
			// Windows 窗体设计器支持所必需的 
			// 
			InitializeComponent(); 
 
			// 
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
			// 
		} 
 
		///  
		/// 清理所有正在使用的资源。 
		///  
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if (components != null)  
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region Windows 窗体设计器生成的代码 
		///  
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改 
		/// 此方法的内容。 
		///  
		private void InitializeComponent() 
		{ 
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmMain)); 
			this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
			this.contextMenu1 = new System.Windows.Forms.ContextMenu(); 
			this.PenColor = new System.Windows.Forms.MenuItem(); 
			this.YNfill = new System.Windows.Forms.MenuItem(); 
			this.mainMenu1 = new System.Windows.Forms.MainMenu(); 
			this.Mnufile = new System.Windows.Forms.MenuItem(); 
			this.Open = new System.Windows.Forms.MenuItem(); 
			this.Save = new System.Windows.Forms.MenuItem(); 
			this.MnuEdit = new System.Windows.Forms.MenuItem(); 
			this.EditPoint = new System.Windows.Forms.MenuItem(); 
			this.EditLine = new System.Windows.Forms.MenuItem(); 
			this.EditEllipse = new System.Windows.Forms.MenuItem(); 
			this.EditRectangle = new System.Windows.Forms.MenuItem(); 
			this.staredit = new System.Windows.Forms.MenuItem(); 
			this.MnuSelect = new System.Windows.Forms.MenuItem(); 
			this.MnuView = new System.Windows.Forms.MenuItem(); 
			this.Zoomout = new System.Windows.Forms.MenuItem(); 
			this.ZoomIn = new System.Windows.Forms.MenuItem(); 
			this.V3 = new System.Windows.Forms.MenuItem(); 
			this.V4 = new System.Windows.Forms.MenuItem(); 
			this.V5 = new System.Windows.Forms.MenuItem(); 
			this.MnuHelp = new System.Windows.Forms.MenuItem(); 
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); 
			this.pictureBox2 = new System.Windows.Forms.PictureBox(); 
			this.axPictureClip1 = new AxPicClip.AxPictureClip(); 
			this.menuItem1 = new System.Windows.Forms.MenuItem(); 
			this.menuItem2 = new System.Windows.Forms.MenuItem(); 
			this.MicHelp = new System.Windows.Forms.MenuItem(); 
			this.SelectR = new System.Windows.Forms.MenuItem(); 
			((System.ComponentModel.ISupportInitialize)(this.axPictureClip1)).BeginInit(); 
			this.SuspendLayout(); 
			//  
			// pictureBox1 
			//  
			this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 
			this.pictureBox1.ContextMenu = this.contextMenu1; 
			this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); 
			this.pictureBox1.Location = new System.Drawing.Point(0, 0); 
			this.pictureBox1.Name = "pictureBox1"; 
			this.pictureBox1.Size = new System.Drawing.Size(800, 600); 
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; 
			this.pictureBox1.TabIndex = 1; 
			this.pictureBox1.TabStop = false; 
			this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp); 
			this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove); 
			this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown); 
			//  
			// contextMenu1 
			//  
			this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { 
																						 this.PenColor, 
																						 this.YNfill, 
																						 this.menuItem1, 
																						 this.menuItem2}); 
			this.contextMenu1.RightToLeft = System.Windows.Forms.RightToLeft.Yes; 
			//  
			// PenColor 
			//  
			this.PenColor.Enabled = false; 
			this.PenColor.Index = 0; 
			this.PenColor.Text = "画笔颜色"; 
			this.PenColor.Click += new System.EventHandler(this.PenColor_Click); 
			//  
			// YNfill 
			//  
			this.YNfill.Enabled = false; 
			this.YNfill.Index = 1; 
			this.YNfill.Text = "填充"; 
			this.YNfill.Click += new System.EventHandler(this.YNfill_Click); 
			//  
			// mainMenu1 
			//  
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { 
																					  this.Mnufile, 
																					  this.MnuEdit, 
																					  this.MnuSelect, 
																					  this.MnuView, 
																					  this.MnuHelp}); 
			//  
			// Mnufile 
			//  
			this.Mnufile.Index = 0; 
			this.Mnufile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { 
																					this.Open, 
																					this.Save}); 
			this.Mnufile.Text = "文件(&F)"; 
			//  
			// Open 
			//  
			this.Open.Index = 0; 
			this.Open.Text = "打开"; 
			this.Open.Click += new System.EventHandler(this.Open_Click); 
			//  
			// Save 
			//  
			this.Save.Index = 1; 
			this.Save.Text = "保存"; 
			this.Save.Click += new System.EventHandler(this.Save_Click); 
			//  
			// MnuEdit 
			//  
			this.MnuEdit.Index = 1; 
			this.MnuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { 
																					this.EditPoint, 
																					this.EditLine, 
																					this.EditEllipse, 
																					this.EditRectangle, 
																					this.staredit}); 
			this.MnuEdit.Text = "编辑(&E)"; 
			//  
			// EditPoint 
			//  
			this.EditPoint.Index = 0; 
			this.EditPoint.Text = "画点"; 
			this.EditPoint.Click += new System.EventHandler(this.EditPoint_Click); 
			//  
			// EditLine 
			//  
			this.EditLine.Index = 1; 
			this.EditLine.Text = "画线"; 
			this.EditLine.Click += new System.EventHandler(this.EditLine_Click); 
			//  
			// EditEllipse 
			//  
			this.EditEllipse.Index = 2; 
			this.EditEllipse.Text = "画圆"; 
			this.EditEllipse.Click += new System.EventHandler(this.EditEllipse_Click); 
			//  
			// EditRectangle 
			//  
			this.EditRectangle.Index = 3; 
			this.EditRectangle.Text = "画矩形"; 
			this.EditRectangle.Click += new System.EventHandler(this.EditRectangle_Click); 
			//  
			// staredit 
			//  
			this.staredit.Index = 4; 
			this.staredit.Text = "关闭编辑"; 
			this.staredit.Click += new System.EventHandler(this.staredit_Click); 
			//  
			// MnuSelect 
			//  
			this.MnuSelect.Index = 2; 
			this.MnuSelect.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { 
																					  this.SelectR}); 
			this.MnuSelect.Text = "选择(&S)"; 
			//  
			// MnuView 
			//  
			this.MnuView.Index = 3; 
			this.MnuView.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { 
																					this.Zoomout, 
																					this.ZoomIn, 
																					this.V3, 
																					this.V4, 
																					this.V5}); 
			this.MnuView.Text = "视图(&V)"; 
			//  
			// Zoomout 
			//  
			this.Zoomout.Index = 0; 
			this.Zoomout.Text = "放大"; 
			this.Zoomout.Click += new System.EventHandler(this.Zoomout_Click); 
			//  
			// ZoomIn 
			//  
			this.ZoomIn.Index = 1; 
			this.ZoomIn.Text = "缩小"; 
			//  
			// V3 
			//  
			this.V3.Index = 2; 
			this.V3.Text = "反色"; 
			this.V3.Click += new System.EventHandler(this.menuItem13_Click); 
			//  
			// V4 
			//  
			this.V4.Index = 3; 
			this.V4.Text = "黑白"; 
			this.V4.Click += new System.EventHandler(this.menuItem14_Click); 
			//  
			// V5 
			//  
			this.V5.Index = 4; 
			this.V5.Text = "原图像"; 
			this.V5.Click += new System.EventHandler(this.V5_Click); 
			//  
			// MnuHelp 
			//  
			this.MnuHelp.Index = 4; 
			this.MnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { 
																					this.MicHelp}); 
			this.MnuHelp.Text = "帮助(&H)"; 
			//  
			// pictureBox2 
			//  
			this.pictureBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)  
				| System.Windows.Forms.AnchorStyles.Right))); 
			this.pictureBox2.Location = new System.Drawing.Point(800, 8); 
			this.pictureBox2.Name = "pictureBox2"; 
			this.pictureBox2.Size = new System.Drawing.Size(232, 174); 
			this.pictureBox2.TabIndex = 3; 
			this.pictureBox2.TabStop = false; 
			//  
			// axPictureClip1 
			//  
			this.axPictureClip1.Enabled = true; 
			this.axPictureClip1.Location = new System.Drawing.Point(640, 0); 
			this.axPictureClip1.Name = "axPictureClip1"; 
			this.axPictureClip1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axPictureClip1.OcxState"))); 
			this.axPictureClip1.TabIndex = 4; 
			//  
			// menuItem1 
			//  
			this.menuItem1.Index = 2; 
			this.menuItem1.Text = "复制"; 
			this.menuItem1.Click += new System.EventHandler(this.Copy_Click); 
			//  
			// menuItem2 
			//  
			this.menuItem2.Index = 3; 
			this.menuItem2.Text = "粘贴"; 
			this.menuItem2.Click += new System.EventHandler(this.Paste_Click); 
			//  
			// MicHelp 
			//  
			this.MicHelp.Index = 0; 
			this.MicHelp.Text = "软件帮助"; 
			this.MicHelp.Click += new System.EventHandler(this.MicHelp_Click); 
			//  
			// SelectR 
			//  
			this.SelectR.Index = 0; 
			this.SelectR.Text = "矩形选择"; 
			this.SelectR.Click += new System.EventHandler(this.SelectR_Click); 
			//  
			// FrmMain 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
			this.ClientSize = new System.Drawing.Size(1026, 447); 
			this.Controls.Add(this.axPictureClip1); 
			this.Controls.Add(this.pictureBox2); 
			this.Controls.Add(this.pictureBox1); 
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; 
			this.Menu = this.mainMenu1; 
			this.Name = "FrmMain"; 
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
			this.Text = "Form1"; 
			((System.ComponentModel.ISupportInitialize)(this.axPictureClip1)).EndInit(); 
			this.ResumeLayout(false); 
 
		} 
		#endregion 
 
		///  
		/// 应用程序的主入口点。 
		///  
		[STAThread] 
		static void Main()  
		{ 
			Application.Run(new FrmMain()); 
		} 
///  
/// 打开 
///  
///  
///  
		private void Open_Click(object sender, System.EventArgs e) 
		{ 
			 
			OpenFileDialog op=new OpenFileDialog(); 
			op.Title="打开文件"; 
			op.ShowDialog (); 
			op.Filter ="图形文件(*.jpg)|*.jpg|图形文件(*.bmp)|*.bmp"; 
			if (op.FileName .Trim ()=="") 
				return; 
			try 
			{ 
				this.pictureBox1 .Image =System.Drawing .Bitmap .FromFile (op.FileName ); 
			} 
			catch(Exception Err) 
			{ 
				MessageBox.Show (Err.Message ); 
			} 
			s=op.FileName; 
			a=true; 
				 
		} 
///  
/// 保存 
///  
///  
///  
		private void Save_Click(object sender, System.EventArgs e) 
		{ 
			SaveFileDialog sf=new SaveFileDialog (); 
			sf.ShowDialog (); 
			sf.FileName =""; 
			sf.Filter ="图形文件(*.jpg)|*.jpg|图形文件(*.bmp)|*.bmp"; 
			if(MessageBox.Show ("需要保存吗?","好",MessageBoxButtons.YesNo ,MessageBoxIcon.Information)==DialogResult.No ) return; 
			if(sf.ShowDialog()==DialogResult.Cancel ) return; 
			string filename=sf.FileName ; 
			this.pictureBox1 .Image .Save (filename+".jpg",System.Drawing.Imaging .ImageFormat .Jpeg  ); 
		} 
		 
		///  
		/// 画点 
		///  
		///  
		///  
		private void EditPoint_Click(object sender, System.EventArgs e) 
		{ 
			this.PenColor .Enabled =true; 
			n=true; 
			MyDrawType=2; 
			this.Refresh (); 
		} 
///  
/// 画线 
///  
///  
///  
		private void EditLine_Click(object sender, System.EventArgs e) 
		{ 
			this.PenColor .Enabled =true; 
			n=true; 
			MyDrawType=0; 
			this.Refresh (); 
		} 
///  
/// 画圆 
///  
///  
///  
		private void EditEllipse_Click(object sender, System.EventArgs e) 
		{ 
			this.PenColor .Enabled =true; 
			this.YNfill .Enabled =true; 
			n=true; 
			 MyDrawType=1; 
			this.Refresh (); 
		} 
		///  
		/// 画矩形 
		///  
		///  
		///  
		private void EditRectangle_Click(object sender, System.EventArgs e) 
		{ 
			this.PenColor .Enabled =true; 
				this.YNfill .Enabled =true; 
			n=true; 
			MyDrawType=3; 
			this.Refresh (); 
		} 
///  
/// 反色 
///  
///  
///  
		private void menuItem13_Click(object sender, System.EventArgs e) 
		{ 
			try 
			{ 
				Bitmap mbt=(Bitmap)this.pictureBox1 .Image ; 
				Color pix=new Color (); 
				for(int i=1;i 
/// 黑白 
///  
///  
///  
		  private void menuItem14_Click(object sender, System.EventArgs e) 
		{ 
			  try 
			  { 
				  Bitmap mbt=(Bitmap)this.pictureBox1 .Image ; 
				  Color pix; 
				  for(int x=0;xg?r:g; 
								  Result=Result>b?Result:b; 
								  break; 
							  case 2:Result=((int)(0.7*r)+(int)(0.2*g)+(int)(0.1*b)); 
								  break; 
						  } 
						  mbt.SetPixel (x,y,Color.FromArgb (Result,Result,Result)); 
					  } 
				  this.pictureBox1 .Image =mbt; 
			  } 
			  catch(Exception Err) 
			  { 
				  MessageBox.Show (Err.ToString ()); 
			  } 
		} 
	///  
	/// MouseDown 
	///  
	///  
	///  
		public void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
		{ 
			if (e.Button ==System.Windows.Forms.MouseButtons.Left) 
			{ 
				pst.X =e.X ; 
				pst.Y =e.Y ; 
			} 
			else 
			{ 
				return; 
			} 
			this.Refresh (); 
		} 
		///  
		/// MouseMove 
		///  
		///  
		///  
		private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
		{ 
			if (e.Button ==System.Windows.Forms.MouseButtons.Left) 
			{ 
				pen.X =e.X ; 
				pen.Y =e.Y ; 
			} 
			Refresh(); 
		} 
///  
/// MouseUP 
///  
///  
///  
		private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
		{ 
			if(n==false) return; 
			if(this.pictureBox1 .Image .Height <1) 
			{ 
				return; 
			} 
			Graphics g=Graphics.FromImage(this.pictureBox1 .Image ); 
			//g.Clear (this.BackColor ); 
			  
			 Pen pen1=new Pen (r,2); 
			 
			 
			if(MyDrawType==0) 
				g.DrawLine (pen1,pst,pen); 
			if(MyDrawType==1) 
				g.DrawEllipse (pen1,pst.X ,pst.Y ,pen.X -pst.X,pen.Y -pst.Y); 
			if(MyDrawType==2) 
				g.DrawRectangle(pen1, pst.X ,pst.Y,1,1); 
			if(MyDrawType==3) 
				g.DrawRectangle(pen1, pst.X ,pst.Y,pen.X- pst.X,pen.Y -pst.Y); 
			this.pictureBox1 .Refresh (); 
			 
		} 
		 
///  
/// 关闭编辑 
///  
///  
///  
		private void staredit_Click(object sender, System.EventArgs e) 
		{ 
			n=false; 
			this.PenColor .Enabled =false; 
			this.YNfill .Enabled =false; 
		} 
///  
/// 选择颜色 
///  
///  
///  
		private void PenColor_Click(object sender, System.EventArgs e) 
		{ 
		    if(n==false) return; 
			ColorDialog clg=new ColorDialog (); 
			if(clg.ShowDialog ()==DialogResult.OK )  r=clg.Color ; 
		} 
///  
/// 填充 
///  
///  
///  
		private void YNfill_Click(object sender, System.EventArgs e) 
		{ 
			if(n==false) return; 
			Graphics g=Graphics.FromImage(this.pictureBox1 .Image ); 
			Rectangle rt=new Rectangle (pst.X ,pst.Y,pen.X- pst.X,pen.Y -pst.Y); 
			SolidBrush brush1=new SolidBrush(r); 
			if(MyDrawType==4) 
				g.FillEllipse(brush1,pst.X ,pst.Y ,pen.X -pst.X,pen.Y -pst.Y); 
			if(MyDrawType==5) 
				g.FillRectangle (brush1,rt); 
			 
		} 
///  
/// 原图像 
///  
///  
///  
		private void V5_Click(object sender, System.EventArgs e) 
		{ 
		   if(s==null) return; 
		   this.pictureBox1 .Image =System.Drawing .Bitmap .FromFile (s); 
		} 
///  
/// 放大 
///  
///  
///  
		private void Zoomout_Click(object sender, System.EventArgs e) 
		{ 
			this.Refresh (); 
		} 
 
		private void Zoomout_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
		{ 
			try 
			{ 
				if(a==false) return; 
				if(this.s.Trim()=="") return; 
				System.Drawing .Bitmap  mb=new Bitmap (this.s); 
				Graphics g=e.Graphics ; 
				TextureBrush mbs=new TextureBrush (mb); 
				float fx=2.0f; 
				float fy=2.0f; 
				g.ScaleTransform (fx,fy); 
				g.FillRectangle (mbs,0,0,this.ClientRectangle .Width ,this.ClientRectangle .Height ); 
			} 
			catch(Exception Err) 
			{ 
				MessageBox.Show (Err.ToString ()); 
			} 
		} 
///  
/// 复制 
///  
///  
///  
		private void Copy_Click(object sender, System.EventArgs e) 
		{ 
			axPictureClip1.Picture =this.pictureBox1 .Image ; 
            axPictureClip1.ClipX =(short)(pst.X) ; 
			axPictureClip1.ClipY =(short)(pst.Y); 
			axPictureClip1.Width =pen.X- pst.X; 
			axPictureClip1.Height =pen.Y -pst.Y; 
			axPictureClip1.Picture =axPictureClip1.get_GraphicCell (0); 
			//Clipboard.SetDataObject (this.pictureBox1 .Image ); 
		} 
///  
/// 粘贴 
///  
///  
///  
		private void Paste_Click(object sender, System.EventArgs e) 
		{ 
		   this.pictureBox2 .Image =this.axPictureClip1.Picture ; 
		} 
///  
/// 帮助 
///  
///  
///  
		private void MicHelp_Click(object sender, System.EventArgs e) 
		{ 
			Help h=new Help (); 
			h.ShowDialog (); 
		} 
 
		private void SelectR_Click(object sender, System.EventArgs e) 
		{ 
	    	Graphics g=Graphics.FromImage(this.pictureBox1 .Image ); 
		     Rectangle rt=new Rectangle (pst.X ,pst.Y,pen.X- pst.X,pen.Y -pst.Y); 
			 
		} 
	 
	} 
}