|  | 
沙发
 
 
 楼主|
发表于 2013-6-28 16:07:22
|
只看该作者 
| [code=csharp] 
 #region 移动鼠标发生的事件
 private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
 {
 if (this.Cursor == Cursors.Cross)
 {
 this.p2 = new Point(e.X, e.Y);
 if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) > 0)     //当鼠标从左上角向开始移动时P3坐标
 {
 this.p3 = new Point(p1.X, p1.Y);
 }
 if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) > 0)     //当鼠标从右上角向左下方向开始移动时P3坐标
 {
 this.p3 = new Point(p2.X, p1.Y);
 }
 if ((p2.X - p1.X) > 0 && (p2.Y - p1.Y) < 0)     //当鼠标从左下角向上开始移动时P3坐标
 {
 this.p3 = new Point(p1.X, p2.Y);
 }
 if ((p2.X - p1.X) < 0 && (p2.Y - p1.Y) < 0)     //当鼠标从右下角向左方向上开始移动时P3坐标
 {
 this.p3 = new Point(p2.X, p2.Y);
 }
 this.pictureBox1.Invalidate();  //使控件的整个图面无效,并导致重绘控件
 }
 }
 #endregion
 #region 松开鼠标发生的事件,实例化ImageCut1类对像
 ImageCut1 IC1;  //定义所画矩形的图像对像
 private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
 {
 if (HeadImageBool)
 {
 width = this.pictureBox1.Image.Width;
 heigth = this.pictureBox1.Image.Height;
 if ((e.X - x1) > 0 && (e.Y - y1) > 0)   //当鼠标从左上角向右下方向开始移动时发生
 {
 IC1 = new ImageCut1(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1));    //实例化ImageCut1类
 }
 if ((e.X - x1) < 0 && (e.Y - y1) > 0)   //当鼠标从右上角向左下方向开始移动时发生
 {
 IC1 = new ImageCut1(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1));   //实例化ImageCut1类
 }
 if ((e.X - x1) > 0 && (e.Y - y1) < 0)   //当鼠标从左下角向右上方向开始移动时发生
 {
 IC1 = new ImageCut1(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1));   //实例化ImageCut1类
 }
 if ((e.X - x1) < 0 && (e.Y - y1) < 0)   //当鼠标从右下角向左上方向开始移动时发生
 {
 IC1 = new ImageCut1(e.X, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1));      //实例化ImageCut1类
 }
 this.pictureBox2.Width = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Width;
 this.pictureBox2.Height = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Height;
 this.pictureBox2.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image));
 this.Cursor = Cursors.Default;
 }
 else
 {
 this.Cursor = Cursors.Default;
 }
 }
 #endregion
 #region 获取所选矩形图像
 /// <summary>
 ///
 /// </summary>
 /// <param name="Width"></param>
 /// <param name="Height"></param>
 /// <returns></returns>
 public Image GetSelectImage1(int Width, int Height)
 {
 Image initImage = this.pictureBox1.Image;
 //Image initImage = Bi;
 //原图宽高均小于模版,不作处理,直接保存
 if (initImage.Width <= Width && initImage.Height <= Height)
 {
 //initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
 return initImage;
 }
 else
 {
 //原始图片的宽、高
 int initWidth = initImage.Width;
 int initHeight = initImage.Height;
 
 //非正方型先裁剪为正方型
 if (initWidth != initHeight)
 {
 //截图对象
 System.Drawing.Image pickedImage = null;
 System.Drawing.Graphics pickedG = null;
 
 //宽大于高的横图
 if (initWidth > initHeight)
 {
 //对象实例化
 pickedImage = new System.Drawing.Bitmap(initHeight, initHeight);
 pickedG = System.Drawing.Graphics.FromImage(pickedImage);
 //设置质量
 pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
 pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
 //定位
 Rectangle fromR = new Rectangle((initWidth - initHeight) / 2, 0, initHeight, initHeight);
 Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
 //画图
 pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
 //重置宽
 initWidth = initHeight;
 }
 //高大于宽的竖图
 else
 {
 //对象实例化
 pickedImage = new System.Drawing.Bitmap(initWidth, initWidth);
 pickedG = System.Drawing.Graphics.FromImage(pickedImage);
 //设置质量
 pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
 pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
 //定位
 Rectangle fromR = new Rectangle(0, (initHeight - initWidth) / 2, initWidth, initWidth);
 Rectangle toR = new Rectangle(0, 0, initWidth, initWidth);
 //画图
 pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
 //重置高
 initHeight = initWidth;
 }
 
 initImage = (System.Drawing.Image)pickedImage.Clone();
 //                //释放截图资源
 pickedG.Dispose();
 pickedImage.Dispose();
 }
 
 return initImage;
 }
 }
 #endregion
 #region 重新绘制pictureBox1控件,即移动鼠标画矩形
 private void pictureBox1_Paint(object sender, PaintEventArgs e)
 {
 if (HeadImageBool)
 {
 Pen p = new Pen(Color.Black, 1);//画笔
 p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
 //Bitmap bitmap = new Bitmap(strHeadImagePath);
 Bitmap bitmap = Bi;
 Rectangle rect = new Rectangle(p3, new Size(System.Math.Abs(p2.X - p1.X), System.Math.Abs(p2.Y - p1.Y)));
 e.Graphics.DrawRectangle(p, rect);
 }
 else
 {
 
 }
 }
 #endregion
 }
 }
 [/code]
 | 
 |