private void OnResize(object sender, EventArgs eventArgs) { if (Image == null) { return; } Transformation = Transformation; }
//public void DecideInitialTransformation() //{ // Transformation = new ProTransformation(Point.Empty, int.MaxValue); //} /// <summary> /// 用于初始显示的时候设置合适的scale /// </summary> /// <returns></returns> private bool SetFitScale() { if (Image == null) { return(false); } var fitScale = CalcFitScale(); Transformation = Transformation.SetScale(fitScale); return(true); }
protected virtual void OnMouseMove(object sender, MouseEventArgs e) { if (_clickedPoint == null) { return; } var p = _transformation.ConvertToIm((Size)e.Location); Transformation = _transformation.SetTranslate(_clickedPoint.Value - p); Invalidate(); }
/// <summary> /// 修正大小,防止太大或者太小 /// </summary> /// <param name="value"></param> /// <returns></returns> private ProTransformation FixTranslation(ProTransformation value) { double maxScale = CalcFitScale(); //最大缩小到跟边框一样大小 if (value.Scale > maxScale) { value = value.SetScale(maxScale); } if (value.Scale < 0.3) { value = value.SetScale(0.3); } var rectSize = value.ConvertToIm(ClientRectangle.Size); var max = new Size(Image.Width - rectSize.Width, Image.Height - rectSize.Height); value = value.SetTranslate((new Point(Math.Min(value.Translation.X, max.Width), Math.Min(value.Translation.Y, max.Height)))); if (value.Translation.X < 0 || value.Translation.Y < 0) { value = value.SetTranslate(new Point(Math.Max(value.Translation.X, 0), Math.Max(value.Translation.Y, 0))); } return(value); }
private void OnMouseWheel(object sender, MouseEventArgs e) { var transformation = _transformation; var pos1 = transformation.ConvertToIm(e.Location); //转换到图像实际的位置 //Console.WriteLine(pos1); //Console.WriteLine(this.Transformation.Scale); if (e.Delta > 0) { transformation = (transformation.SetScale(Transformation.Scale / 1.25)); } else { transformation = (transformation.SetScale(Transformation.Scale * 1.25)); } ////这里再处理一下缩小到了小于等于 var pos2 = transformation.ConvertToIm(e.Location); transformation = transformation.AddTranslate(pos1 - (Size)pos2); Transformation = transformation; Invalidate(); }
public ProPictureBox() { _transformation = new ProTransformation(new Point(100, 0), .5f); MouseDown += OnMouseDown; MouseMove += OnMouseMove; MouseUp += OnMouseUp; MouseWheel += OnMouseWheel; Resize += OnResize; ImageChanged += OnImageChanged; #region 右键菜单初始化 this.cmsPicBox = new System.Windows.Forms.ContextMenuStrip(); this.保存图像ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.旋转图像ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.左旋90度ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.右旋90度ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.垂直翻转ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.水平翻转ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); // // cmsPicBox // this.cmsPicBox.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.保存图像ToolStripMenuItem, this.旋转图像ToolStripMenuItem }); this.cmsPicBox.Name = "cmsPicBox"; this.cmsPicBox.Size = new System.Drawing.Size(153, 70); // // 保存图像ToolStripMenuItem // this.保存图像ToolStripMenuItem.Name = "保存图像ToolStripMenuItem"; this.保存图像ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.保存图像ToolStripMenuItem.Text = "保存图像"; this.保存图像ToolStripMenuItem.Click += new System.EventHandler(this.保存图像ToolStripMenuItem_Click); // // 旋转图像ToolStripMenuItem // this.旋转图像ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.左旋90度ToolStripMenuItem, this.右旋90度ToolStripMenuItem, this.垂直翻转ToolStripMenuItem, this.水平翻转ToolStripMenuItem }); this.旋转图像ToolStripMenuItem.Name = "旋转图像ToolStripMenuItem"; this.旋转图像ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.旋转图像ToolStripMenuItem.Text = "旋转图像"; // // 左旋90度ToolStripMenuItem // this.左旋90度ToolStripMenuItem.Name = "左旋90度ToolStripMenuItem"; this.左旋90度ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.左旋90度ToolStripMenuItem.Text = "左旋90度"; this.左旋90度ToolStripMenuItem.Click += new System.EventHandler(this.左旋90度ToolStripMenuItem_Click); // // 右旋90度ToolStripMenuItem // this.右旋90度ToolStripMenuItem.Name = "右旋90度ToolStripMenuItem"; this.右旋90度ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.右旋90度ToolStripMenuItem.Text = "右旋90度"; this.右旋90度ToolStripMenuItem.Click += new System.EventHandler(this.右旋90度ToolStripMenuItem_Click); // // 垂直翻转ToolStripMenuItem // this.垂直翻转ToolStripMenuItem.Name = "垂直翻转ToolStripMenuItem"; this.垂直翻转ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.垂直翻转ToolStripMenuItem.Text = "垂直翻转"; this.垂直翻转ToolStripMenuItem.Click += new System.EventHandler(this.垂直翻转ToolStripMenuItem_Click); // // 水平翻转ToolStripMenuItem // this.水平翻转ToolStripMenuItem.Name = "水平翻转ToolStripMenuItem"; this.水平翻转ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.水平翻转ToolStripMenuItem.Text = "水平翻转"; this.水平翻转ToolStripMenuItem.Click += new System.EventHandler(this.水平翻转ToolStripMenuItem_Click); #endregion }