请推荐一个图片查看控件 要有平移,放大,缩小功能(是通过鼠标拖拉完成,而不是那种点一下就按固定倍数放大)可以是第三方,也可以是picturebox加代码实现谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 pictureBox1.SizeMode = PictureBoxSizeMode.Zoomprivate void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (pictureBox1.Width < 1000) { pictureBox1.Width = Convert.ToInt32(pictureBox1.Width * 1.2); pictureBox1.Height = Convert.ToInt32(pictureBox1.Height * 1.2); } } if (e.Button == MouseButtons.Right) { if (pictureBox1.Width > 600) { pictureBox1.Width = Convert.ToInt32(pictureBox1.Width / 1.2); pictureBox1.Height = Convert.ToInt32(pictureBox1.Height / 1.2); } } }http://greatmaps.codeplex.com/ 以下都能滿足http://www.gdpicture.com/orhttp://www.gdpicture.com/orhttp://www.atalasoft.com而且是非常強大,picturebox 是垃圾my bloghttp://ufo-crackerx.blog.163.com/ 最终还是在picturebox的基础上自己用代码实现了,现在把代码贴出来,望各位高手批评指教 using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms;namespace PlugManage.AdvancedTools.AccessoriesManage{ /// <summary> /// picViewer控件的操作状态 /// </summary> public enum picViewerActionType { /// <summary> /// 平移 /// </summary> Pan = 0, /// <summary> /// 放大 /// </summary> ZoomIn = 1, /// <summary> /// 缩小 /// </summary> ZoomOut = 2 } /// <summary> /// 图片简单浏览控件 /// </summary> public class picViewer : System.Windows.Forms.UserControl { private System.Windows.Forms.PictureBox pictureBox1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; /// <summary> /// 图片控件的操作类型 /// </summary> private picViewerActionType _actionType = 0; /// <summary> /// 图片控件的操作类型 /// </summary> public picViewerActionType ActionType { get { return _actionType; } set { this._actionType = value; //设置鼠标 if (this._actionType == picViewerActionType.ZoomIn) { this.pictureBox1.Cursor = this.m_zoomin; } else if (this._actionType == picViewerActionType.ZoomOut) { this.pictureBox1.Cursor = this.m_zoomout; } else if (this._actionType == picViewerActionType.Pan) { this.pictureBox1.Cursor = this.m_pan; } } } /// <summary> /// 图片路径 /// </summary> private string m_picfile; private System.Drawing.Image m_image; /// <summary> /// 当前要显示的图片路径 /// </summary> public string PicFile { set { if(value != "" && System.IO.File.Exists(value)) { try { this.m_picfile = value; if(m_image!= null) m_image.Dispose(); this.m_image = System.Drawing.Image.FromFile(this.m_picfile); this.Full(); } catch(Exception ex) { MessageBox.Show("您指定的文件并非图片!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); } } else { this.m_picfile = ""; if (m_image != null) { this.m_image.Dispose(); m_image = null; } this.pictureBox1.Refresh(); } } get { return m_picfile; } } /// <summary> /// /// </summary> public System.IO.Stream BackImage { set { this.pictureBox1.SizeMode = PictureBoxSizeMode.Normal; //this.pictureBox1.Image = Image.FromStream(value); } } /// <summary> /// 图片框的画图区域 /// </summary> private Rectangle m_curDrawRect; /// <summary> /// 图片的显示区域 /// </summary> private Rectangle m_curImgRect; /// <summary> /// 拉框放大的区域 /// </summary> private Rectangle m_autoZoom; private Point m_padPoint; private Point m_zoomPoint; private Rectangle m_TrackRectangle = new Rectangle(new Point(0, 0), new Size(0, 0)); /// <summary> /// 光标 /// </summary> private Cursor m_pan; private Cursor m_paning; private Cursor m_zoomin; private Cursor m_zoomout; /// <summary> /// 背景色 /// </summary> private Color m_backColor = Color.White; public override Color BackColor { get { return m_backColor; } set { m_backColor = value; } } public picViewer() { // 该调用是 Windows.Forms 窗体设计器所必需的。 InitializeComponent(); m_pan = new Cursor(GetType().Assembly.GetManifestResourceStream("CommandToolSample.PlugManage.AdvancedTools.Cursor.Pan.cur")); m_paning = new Cursor(GetType().Assembly.GetManifestResourceStream("CommandToolSample.PlugManage.AdvancedTools.Cursor.Paning.cur")); m_zoomin = new Cursor(GetType().Assembly.GetManifestResourceStream("CommandToolSample.PlugManage.AdvancedTools.Cursor.ZoomIn.cur")); m_zoomout = new Cursor(GetType().Assembly.GetManifestResourceStream("CommandToolSample.PlugManage.AdvancedTools.Cursor.ZoomOut.cur")); m_backColor = Color.White; // TODO: 在 InitializeComponent 调用后添加任何初始化 } public void DisposeImage() { if(this.m_image != null) this.m_image.Dispose(); } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { this.pictureBox1.Dispose(); components.Dispose(); } } base.Dispose( disposing ); } private void picViewer_Load(object sender, System.EventArgs e) { // this.Full(); ///默认的操方式为平移 this.ActionType = picViewerActionType.Pan; } private void pictureBox1_Resize(object sender, System.EventArgs e) { if (this.Width < 200) { this.Width = 200; } if (this.Height < 200) { this.Height = 200; } if (this.m_image == null) return; // this.pictureBox1.Refresh(); //this.Full(); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// </summary> private void InitializeComponent() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(600, 360); this.pictureBox1.TabIndex = 1; this.pictureBox1.TabStop = false; this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove); this.pictureBox1.Resize += new System.EventHandler(this.pictureBox1_Resize); this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown); this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp); // // picViewer // this.Controls.Add(this.pictureBox1); this.Name = "picViewer"; this.Size = new System.Drawing.Size(600, 360); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.ResumeLayout(false); } #endregion #region 对外功能 /// <summary> /// 放大操作 /// </summary> public void ZoomIn() { if(this.m_image == null) return; //计算放大的范围和画图的坐标 int newht = (int)(this.m_curDrawRect.Height * 1.2); int newwt = (int)(this.m_curDrawRect.Width * 1.2); int ox = (int)((newwt - this.m_curDrawRect.Width)/2); int oy = (int)((newht - this.m_curDrawRect.Height)/2); int x = this.m_curDrawRect.X - ox; int y = this.m_curDrawRect.Y - oy; //如果放大超过实际图片大小的倍数则放弃操作 if((newht/this.m_image.Height)>5||(newwt/this.m_image.Width)>5) return; this.m_curDrawRect = new Rectangle(x,y,newwt,newht); this.pictureBox1.Invalidate(); } /// <summary> /// 缩小 /// </summary> public void ZoomOut() { if(this.m_image == null) return; //计算放大的范围和画图的坐标 int pbh = this.pictureBox1.Height; int pbw = this.pictureBox1.Width; int newht = (int)(this.m_curDrawRect.Height / 1.2); int newwt = (int)(this.m_curDrawRect.Width / 1.2); int ox = (int)((this.m_curDrawRect.Width - newwt)/2); int oy = (int)((this.m_curDrawRect.Height - newht)/2); int x = this.m_curDrawRect.X + ox; int y = this.m_curDrawRect.Y + oy; //判断边缘 if(newwt>pbw) { if(x>0) x=0; if((x+newwt)<pbw) x=pbw-newwt; } else { x = (int)((pbw-newwt)/2); } if(newht>pbh) { if(y>0) y=0; if((y+newht)<pbh) y=pbh-newht; } else { y = (int)((pbh-newht)/2); } //如果缩小小于图片框大小的倍数则放弃操作 if((this.pictureBox1.Height/newht)>5||(this.pictureBox1.Width/newwt)>5) return; this.m_curDrawRect = new Rectangle(x,y,newwt,newht); this.pictureBox1.Invalidate(); } /// <summary> /// 全图显示 /// </summary> public void Full() { if (this.m_image == null) return; int pbh = this.pictureBox1.Height; int pbw = this.pictureBox1.Width; int ht = this.m_image.Height; int wt = this.m_image.Width; int newht = pbh; int newwt = pbw; int x = 0; int y = 0; //计算画图坐标和显示区域大小 if (ht < pbh && wt < pbw)//图片高宽均小于图片框 { newht = ht; newwt = wt; x = (pbw - wt) / 2; y = (pbh - ht) / 2; } else if (ht > pbh && wt <= pbw)//图片高>图片框高,宽<=图片框宽 { newht = pbh; newwt = (int)((pbh / (ht * 1.0)) * wt); x = (pbw - newwt) / 2; y = 0; } else if (ht <= pbh && wt > pbw)//反过来 { newht = (int)((pbw / (wt * 1.0)) * ht); newwt = pbw; x = 0; y = (pbh - newht) / 2; } else//图片高宽均大于图片框 { if ((ht / (pbh * 1.0)) > (wt / (pbw * 1.0)))//同比高大于宽 { newht = pbh; newwt = (int)((pbh / (ht * 1.0)) * wt); x = (pbw - newwt) / 2; y = 0; } else//同比宽大于高 { newht = (int)((pbw / (wt * 1.0)) * ht); newwt = pbw; x = 0; y = (pbh - newht) / 2; } } this.m_curDrawRect = new Rectangle(x, y, newwt, newht); this.m_curImgRect = new Rectangle(0, 0, wt, ht); this.pictureBox1.Invalidate(); } /// <summary> /// 原图大小显示 /// </summary> public void Zoom() { if (this.m_image == null) return; int pbh = this.pictureBox1.Height; int pbw = this.pictureBox1.Width; int ht = this.m_image.Height; int wt = this.m_image.Width; int newht = pbh; int newwt = pbw; int x = 0; int y = 0; //计算画图坐标和显示区域大小 if (ht < pbh && wt < pbw)//图片高宽均小于图片框 { newht = ht; newwt = wt; x = (pbw - wt) / 2; y = (pbh - ht) / 2; } else if (ht > pbh && wt <= pbw)//图片高>图片框高,宽<=图片框宽 { x = (pbw - wt) / 2; y = 0 - (ht - pbh) / 2; newht = ht; newwt = wt; } else if (ht <= pbh && wt > pbw)//反过来 { x = 0 - (wt - pbw) / 2; y = (pbh - ht) / 2; newht = ht; newwt = wt; } else//图片高宽均大于图片框 { x = 0 - (wt - pbw) / 2; y = 0 - (ht - pbh) / 2; newht = ht; newwt = wt; } this.m_curDrawRect = new Rectangle(x, y, newwt, newht); this.m_curImgRect = new Rectangle(0, 0, wt, ht); this.pictureBox1.Invalidate(); } #endregion #region 放大缩小平移具体实现函数 /// <summary> /// 拉框放大 /// </summary> private void AutoZoomIn() { if (this.m_image == null) return; //picturebox控件范围 double pbh = this.pictureBox1.Height; double pbw = this.pictureBox1.Width; //图片实际范围 double drawh = this.m_curDrawRect.Height; double draww = this.m_curDrawRect.Width; double drawX = this.m_curDrawRect.X; double drawY = this.m_curDrawRect.Y; //图片原始范围 double imgH = this.m_image.Height; double imgW = this.m_image.Width; //zoom框范围 double zoomh = this.m_autoZoom.Height; double zoomw = this.m_autoZoom.Width; double zoomX = this.m_autoZoom.X; double zoomY = this.m_autoZoom.Y; //Zoom矩形不能是一条线或点,也就是width和height要大于0 if (zoomh <= 0 || zoomw <= 0) return; //zoom框宽高比 double zoomWHScale = (double)zoomw / (double)zoomh; //picturebox宽高比 double picboxWHScale = (double)pbw / (double)pbh; //把zoom框以picturebox的范围当做最大范围,进行缩放 double fillZoomX = 0, fillZoomY = 0, fillZoomW = 0, fillZoomH = 0; if (zoomWHScale > picboxWHScale) { fillZoomX = 0; fillZoomW = pbw; double zoomInScale = fillZoomW / zoomw; fillZoomH = zoomh * zoomInScale; fillZoomY = (pbh - fillZoomH) / 2; } else { fillZoomY = 0; fillZoomH = pbh; double zoomInScale = fillZoomH / zoomh; fillZoomW = zoomw * zoomInScale; fillZoomX = (pbw - fillZoomW) / 2; } //zoom框与图片实际显示范围的交集 Rectangle oldZoomDraw = Rectangle.Intersect(this.m_autoZoom, this.m_curDrawRect); //矩形交集为空时跳出 if (oldZoomDraw.Width <= 0 || oldZoomDraw.Height <= 0 || oldZoomDraw.X > pbw || oldZoomDraw.Y > pbh) return; //缩放后的zoom框与zoom框的宽高比 double zoomXScale = fillZoomW / zoomw; double zoomYScale = fillZoomH / zoomh; //缩放后的zoom框中的图片实际显示范围 double newZoomDrawX = (oldZoomDraw.X - zoomX) * zoomXScale + fillZoomX; double newZoomDrawY = (oldZoomDraw.Y - zoomY) * zoomYScale + fillZoomY; double newZoomDrawW = oldZoomDraw.Width * zoomXScale; double newZoomDrawH = oldZoomDraw.Height * zoomYScale; //图片实际显示与图片原始的宽高比 double oldZoomImgXScale = draww / imgW; double oldZoomImgYScale = drawh / imgH; //zoom框中图片的范围(以图片原始大小为单位) double ZoomImgX = (oldZoomDraw.X - drawX) / oldZoomImgXScale; double ZoomImgY = (oldZoomDraw.Y - drawY) / oldZoomImgYScale; double ZoomImgW = oldZoomDraw.Width / oldZoomImgXScale; double ZoomImgH = oldZoomDraw.Height / oldZoomImgYScale; //缩放后图片实际显示与图片原始的宽高比 double newZoomImgXScale = newZoomDrawW / ZoomImgW; double newZoomImgYScale = newZoomDrawH / ZoomImgH; //最终图片的实际显示范围 double newDrawX = newZoomDrawX - ZoomImgX * newZoomImgXScale; double newDrawY = newZoomDrawY - ZoomImgY * newZoomImgYScale; double newDrawW = imgW * newZoomImgXScale; double newDrawH = imgH * newZoomImgYScale; //如果缩小小于图片框大小的倍数则放弃操作 if (((double)this.pictureBox1.Height / newDrawH) < 0.1 || ((double)this.pictureBox1.Width / newDrawW) < 0.1) return; this.m_curDrawRect = new Rectangle((int)Math.Truncate(newDrawX), (int)Math.Truncate(newDrawY), (int)Math.Truncate(newDrawW), (int)Math.Truncate(newDrawH)); this.pictureBox1.Invalidate(); } #region MouseDown,MouseMove,MouseUp三大事件函数 private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { //非左键不操作 if(e.Button != MouseButtons.Left) return; if (this._actionType == picViewerActionType.ZoomIn || this._actionType == picViewerActionType.ZoomOut) { if (this.m_autoZoom == Rectangle.Empty) { //判断鼠标是否落在图片绘图区 if ((this._actionType == picViewerActionType.ZoomIn && this.m_curDrawRect.Contains(e.X, e.Y)) || (this._actionType == picViewerActionType.ZoomOut && e.X <= this.pictureBox1.Width && e.Y <= this.pictureBox1.Height)) { this.m_padPoint = Point.Empty; this.m_zoomPoint = new Point(e.X, e.Y); } else { this.m_padPoint = Point.Empty; this.m_zoomPoint = Point.Empty; } } } else if (this._actionType == picViewerActionType.Pan) { this.m_padPoint = new Point(e.X, e.Y); this.m_zoomPoint = Point.Empty; Cursor.Current = this.m_paning; } } private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (this._actionType == picViewerActionType.ZoomIn || this._actionType == picViewerActionType.ZoomOut) { if (this.m_zoomPoint != Point.Empty)//拉框放大,画虚线框 { ControlPaint.DrawReversibleFrame(m_TrackRectangle, this.BackColor, FrameStyle.Dashed); //鼠标不能超过的范围 Rectangle standardRect; if (this._actionType == picViewerActionType.ZoomIn) standardRect = this.m_curDrawRect; else standardRect = new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height); Point tmpPoint = new Point(e.X, e.Y); if (!standardRect.Contains(tmpPoint)) { if (tmpPoint.X < standardRect.Left) tmpPoint.X = standardRect.Left; if (tmpPoint.X > standardRect.Right) tmpPoint.X = standardRect.Right; if (tmpPoint.Y < standardRect.Top) tmpPoint.Y = standardRect.Top; if (tmpPoint.Y > standardRect.Bottom) tmpPoint.Y = standardRect.Bottom; } Point startPoint = this.PointToScreen(this.m_zoomPoint); Point endPoint = this.PointToScreen(tmpPoint); int width = endPoint.X - startPoint.X; int height = endPoint.Y - startPoint.Y; m_TrackRectangle = new Rectangle(startPoint.X, startPoint.Y, width, height); ControlPaint.DrawReversibleFrame(m_TrackRectangle, this.BackColor, FrameStyle.Dashed); } } else if (this._actionType == picViewerActionType.Pan) { Point thisPoint = new Point(e.X, e.Y); Rectangle standardRect = new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height); //超出picturebox就不处理 if (this.m_padPoint != Point.Empty && standardRect.Contains(thisPoint))//平移 { this.Pad(thisPoint); this.m_padPoint = thisPoint; } } } private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button != MouseButtons.Left) return; Point toPoint = new Point(e.X, e.Y); if (this._actionType == picViewerActionType.ZoomIn || this._actionType == picViewerActionType.ZoomOut) { if (this.m_zoomPoint != Point.Empty)//拉框放大 { //鼠标不能超过的范围 Rectangle standardRect; if (this._actionType == picViewerActionType.ZoomIn) standardRect = this.m_curDrawRect; else standardRect = new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height); Point tmpPoint = new Point(e.X, e.Y); if (!standardRect.Contains(tmpPoint)) { if (tmpPoint.X < standardRect.Left) tmpPoint.X = standardRect.Left; if (tmpPoint.X > standardRect.Right) tmpPoint.X = standardRect.Right; if (tmpPoint.Y < standardRect.Top) tmpPoint.Y = standardRect.Top; if (tmpPoint.Y > standardRect.Bottom) tmpPoint.Y = standardRect.Bottom; } int x = this.m_zoomPoint.X; int y = this.m_zoomPoint.Y; int width = Math.Abs(toPoint.X - x); int height = Math.Abs(toPoint.Y - y); if (x > toPoint.X) x = toPoint.X; if (y > toPoint.Y) y = toPoint.Y; this.m_autoZoom = new Rectangle(x, y, width, height); this.m_zoomPoint = Point.Empty; //取消掉图片上的临时框 ControlPaint.DrawReversibleFrame(m_TrackRectangle, this.BackColor, FrameStyle.Dashed); m_TrackRectangle = new Rectangle(0, 0, 0, 0); ///放大和缩小 if (this._actionType == picViewerActionType.ZoomIn) this.AutoZoomIn(); else if (this._actionType == picViewerActionType.ZoomOut) this.AutoZoomOut(); this.m_autoZoom = Rectangle.Empty; } } else if (this._actionType == picViewerActionType.Pan) { if (this.m_padPoint != Point.Empty)//平移 { this.m_padPoint = Point.Empty; } this.Cursor = this.m_pan; } } #endregion private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { //无图片时不执行操作 if(this.m_image == null) return; //初始化显示图片 if(this.m_curDrawRect == Rectangle.Empty || this.m_curImgRect == Rectangle.Empty) { this.Full(); return; } try { //判断拉框放大的框是否还存在 if (this.m_autoZoom != Rectangle.Empty) { ControlPaint.DrawReversibleFrame(m_TrackRectangle, this.BackColor, FrameStyle.Dashed); m_TrackRectangle = new Rectangle(0, 0, 0, 0); this.m_autoZoom = Rectangle.Empty; } //设置高质量,低速度呈现平滑程度 Graphics grap = e.Graphics; // grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; // grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // grap.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //清空一下画布 grap.Clear(m_backColor); grap.DrawImage(this.m_image, this.m_curDrawRect, this.m_curImgRect, GraphicsUnit.Pixel); } catch (Exception ex) { } base.OnPaint(e); } public void refresh() { SetStyle(ControlStyles.SupportsTransparentBackColor,true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); Refresh(); } }} 建一个名称为picViewer的控件,把代码copy过去就能用了(因为字数限制,所以把代码分开几楼发)这样子把图片加载到控件picViewer1.PicFile = filepath,filepath是图片的文件路径向外提供this.picViewer1.Full()(全屏显示),this.picViewer1.Zoom()(图片实际大小显示)通过this.picViewer1.ActionType = picViewerActionType.ZoomIn;this.picViewer1.ActionType = picViewerActionType.ZoomOut;this.picViewer1.ActionType = picViewerActionType.Pan;控制鼠标操作是平移还是放大还是缩小 樓主,少了this.Pad(thisPoint);this.AutoZoomOut()這两个函数呀,方便提供上来吗? /// <summary> /// 拉框缩小 /// </summary> private void AutoZoomOut() { if (this.m_image == null) return; //各个高宽取值 double pbh = this.pictureBox1.Height; double pbw = this.pictureBox1.Width; double drawh = this.m_curDrawRect.Height; double draww = this.m_curDrawRect.Width; double drawX = this.m_curDrawRect.X; double drawY = this.m_curDrawRect.Y; double imgH = this.m_image.Height; double imgW = this.m_image.Width; double zoomh = this.m_autoZoom.Height; double zoomw = this.m_autoZoom.Width; double zoomX = this.m_autoZoom.X; double zoomY = this.m_autoZoom.Y; //Zoom矩形不能是一条线或点,也就是width和height要大于0 if (zoomh <= 0 || zoomw <= 0) return; double zoomWHScale = (double)zoomw / (double)zoomh; double picboxWHScale = (double)pbw / (double)pbh; double fillZoomX = 0, fillZoomY = 0, fillZoomW = 0, fillZoomH = 0; if (zoomWHScale > picboxWHScale) { fillZoomX = 0; fillZoomW = pbw; double zoomInScale = fillZoomW / zoomw; fillZoomH = zoomh * zoomInScale; fillZoomY = (pbh - fillZoomH) / 2; } else { fillZoomY = 0; fillZoomH = pbh; double zoomInScale = fillZoomH / zoomh; fillZoomW = zoomw * zoomInScale; fillZoomX = (pbw - fillZoomW) / 2; } Rectangle fillZoom = new Rectangle((int)Math.Truncate(fillZoomX), (int)Math.Truncate(fillZoomY), (int)Math.Truncate(fillZoomW), (int)Math.Truncate(fillZoomH)); Rectangle oldZoomDraw = Rectangle.Intersect(fillZoom, this.m_curDrawRect); //矩形交集为空时跳出 if (oldZoomDraw.Width <= 0 || oldZoomDraw.Height <= 0 || oldZoomDraw.X > pbw || oldZoomDraw.Y > pbh) return; double zoomXScale = fillZoomW / zoomw; double zoomYScale = fillZoomH / zoomh; double newZoomDrawX = (oldZoomDraw.X - fillZoomX) / zoomXScale + zoomX; double newZoomDrawY = (oldZoomDraw.Y - fillZoomY) / zoomYScale + zoomY; double newZoomDrawW = oldZoomDraw.Width / zoomXScale; double newZoomDrawH = oldZoomDraw.Height / zoomYScale; double oldZoomImgXScale = draww / imgW; double oldZoomImgYScale = drawh / imgH; double ZoomImgX = (oldZoomDraw.X - drawX) / oldZoomImgXScale; double ZoomImgY = (oldZoomDraw.Y - drawY) / oldZoomImgYScale; double ZoomImgW = oldZoomDraw.Width / oldZoomImgXScale; double ZoomImgH = oldZoomDraw.Height / oldZoomImgYScale; double newZoomImgXScale = newZoomDrawW / ZoomImgW; double newZoomImgYScale = newZoomDrawH / ZoomImgH; double newDrawX = newZoomDrawX - ZoomImgX * newZoomImgXScale; double newDrawY = newZoomDrawY - ZoomImgY * newZoomImgYScale; double newDrawW = imgW * newZoomImgXScale; double newDrawH = imgH * newZoomImgYScale; //如果缩小小于图片框大小的倍数则放弃操作 if (((double)this.pictureBox1.Height / newDrawH) > 10 || ((double)this.pictureBox1.Width / newDrawW) > 10) return; this.m_curDrawRect = new Rectangle((int)Math.Truncate(newDrawX), (int)Math.Truncate(newDrawY), (int)Math.Truncate(newDrawW), (int)Math.Truncate(newDrawH)); this.pictureBox1.Invalidate(); } /// <summary> /// 平移 /// </summary> /// <param name="toPoint"></param> private void Pad(Point toPoint) { int pbh = this.pictureBox1.Height; int pbw = this.pictureBox1.Width; int newht = this.m_curDrawRect.Height; int newwt = this.m_curDrawRect.Width; int x = this.m_curDrawRect.X + (toPoint.X - this.m_padPoint.X); int y = this.m_curDrawRect.Y + (toPoint.Y - this.m_padPoint.Y); if(this.m_curDrawRect.X == x && this.m_curDrawRect.Y == y) return; Rectangle newDrawRect = new Rectangle(x, y, newwt, newht); //如果图片完全不在picturebox中则不处理 Rectangle picBoxRect = new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height); if (newDrawRect.IntersectsWith(picBoxRect) == false) return; this.m_curDrawRect = newDrawRect; this.pictureBox1.Invalidate(); } 请教各位如何把WinForm窗体总是前置在所有应用程序之前 简单的调色板问题 UDP的NAT穿透 急啊,有关罗马字符和汉字的处理 按钮单击事件使图像信息保存到listview中 C#和C++哪好呀? webform中创建了tcplistener ,但是客户端连不上 异步发送大数据量,肯定要分包,但是在接收方怎么组包? 大家帮忙看看呀! 知道汉字如何变为ASCII码吗? c# winform 上传文件的控件是什么? 关于窗体响应问题
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (pictureBox1.Width < 1000)
{
pictureBox1.Width = Convert.ToInt32(pictureBox1.Width * 1.2);
pictureBox1.Height = Convert.ToInt32(pictureBox1.Height * 1.2);
}
}
if (e.Button == MouseButtons.Right)
{
if (pictureBox1.Width > 600)
{
pictureBox1.Width = Convert.ToInt32(pictureBox1.Width / 1.2);
pictureBox1.Height = Convert.ToInt32(pictureBox1.Height / 1.2);
}
}
}http://greatmaps.codeplex.com/
orhttp://www.gdpicture.com/orhttp://www.atalasoft.com而且是非常強大,picturebox 是垃圾
my blog
http://ufo-crackerx.blog.163.com/
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace PlugManage.AdvancedTools.AccessoriesManage
{
/// <summary>
/// picViewer控件的操作状态
/// </summary>
public enum picViewerActionType
{
/// <summary>
/// 平移
/// </summary>
Pan = 0,
/// <summary>
/// 放大
/// </summary>
ZoomIn = 1,
/// <summary>
/// 缩小
/// </summary>
ZoomOut = 2
} /// <summary>
/// 图片简单浏览控件
/// </summary>
public class picViewer : System.Windows.Forms.UserControl
{
private System.Windows.Forms.PictureBox pictureBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; /// <summary>
/// 图片控件的操作类型
/// </summary>
private picViewerActionType _actionType = 0;
/// <summary>
/// 图片控件的操作类型
/// </summary>
public picViewerActionType ActionType
{
get { return _actionType; }
set
{
this._actionType = value; //设置鼠标
if (this._actionType == picViewerActionType.ZoomIn)
{
this.pictureBox1.Cursor = this.m_zoomin;
}
else if (this._actionType == picViewerActionType.ZoomOut)
{
this.pictureBox1.Cursor = this.m_zoomout;
}
else if (this._actionType == picViewerActionType.Pan)
{
this.pictureBox1.Cursor = this.m_pan;
}
}
} /// <summary>
/// 图片路径
/// </summary>
private string m_picfile;
private System.Drawing.Image m_image;
/// <summary>
/// 当前要显示的图片路径
/// </summary>
public string PicFile
{
set
{
if(value != "" && System.IO.File.Exists(value))
{
try
{
this.m_picfile = value;
if(m_image!= null)
m_image.Dispose();
this.m_image = System.Drawing.Image.FromFile(this.m_picfile);
this.Full();
}
catch(Exception ex)
{
MessageBox.Show("您指定的文件并非图片!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
else
{
this.m_picfile = "";
if (m_image != null)
{
this.m_image.Dispose();
m_image = null;
}
this.pictureBox1.Refresh();
}
}
get
{
return m_picfile;
}
} /// <summary>
///
/// </summary>
public System.IO.Stream BackImage
{
set
{
this.pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
//this.pictureBox1.Image = Image.FromStream(value);
}
} /// <summary>
/// 图片框的画图区域
/// </summary>
private Rectangle m_curDrawRect;
/// <summary>
/// 图片的显示区域
/// </summary>
private Rectangle m_curImgRect;
/// <summary>
/// 拉框放大的区域
/// </summary>
private Rectangle m_autoZoom;
private Point m_padPoint;
private Point m_zoomPoint;
private Rectangle m_TrackRectangle = new Rectangle(new Point(0, 0), new Size(0, 0)); /// <summary>
/// 光标
/// </summary>
private Cursor m_pan;
private Cursor m_paning;
private Cursor m_zoomin;
private Cursor m_zoomout; /// <summary>
/// 背景色
/// </summary>
private Color m_backColor = Color.White;
public override Color BackColor
{
get
{
return m_backColor;
} set
{
m_backColor = value;
}
} public picViewer()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent(); m_pan = new Cursor(GetType().Assembly.GetManifestResourceStream("CommandToolSample.PlugManage.AdvancedTools.Cursor.Pan.cur"));
m_paning = new Cursor(GetType().Assembly.GetManifestResourceStream("CommandToolSample.PlugManage.AdvancedTools.Cursor.Paning.cur"));
m_zoomin = new Cursor(GetType().Assembly.GetManifestResourceStream("CommandToolSample.PlugManage.AdvancedTools.Cursor.ZoomIn.cur"));
m_zoomout = new Cursor(GetType().Assembly.GetManifestResourceStream("CommandToolSample.PlugManage.AdvancedTools.Cursor.ZoomOut.cur")); m_backColor = Color.White;
// TODO: 在 InitializeComponent 调用后添加任何初始化
} public void DisposeImage()
{
if(this.m_image != null)
this.m_image.Dispose();
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
this.pictureBox1.Dispose();
components.Dispose();
}
}
base.Dispose( disposing );
} private void picViewer_Load(object sender, System.EventArgs e)
{
// this.Full(); ///默认的操方式为平移
this.ActionType = picViewerActionType.Pan;
} private void pictureBox1_Resize(object sender, System.EventArgs e)
{
if (this.Width < 200)
{
this.Width = 200;
}
if (this.Height < 200)
{
this.Height = 200;
} if (this.m_image == null)
return; // this.pictureBox1.Refresh();
//this.Full();
} #region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(600, 360);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.Resize += new System.EventHandler(this.pictureBox1_Resize);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
//
// picViewer
//
this.Controls.Add(this.pictureBox1);
this.Name = "picViewer";
this.Size = new System.Drawing.Size(600, 360);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false); }
#endregion #region 对外功能 /// <summary>
/// 放大操作
/// </summary>
public void ZoomIn()
{
if(this.m_image == null)
return; //计算放大的范围和画图的坐标
int newht = (int)(this.m_curDrawRect.Height * 1.2);
int newwt = (int)(this.m_curDrawRect.Width * 1.2);
int ox = (int)((newwt - this.m_curDrawRect.Width)/2);
int oy = (int)((newht - this.m_curDrawRect.Height)/2);
int x = this.m_curDrawRect.X - ox;
int y = this.m_curDrawRect.Y - oy;
//如果放大超过实际图片大小的倍数则放弃操作
if((newht/this.m_image.Height)>5||(newwt/this.m_image.Width)>5)
return; this.m_curDrawRect = new Rectangle(x,y,newwt,newht);
this.pictureBox1.Invalidate();
} /// <summary>
/// 缩小
/// </summary>
public void ZoomOut()
{
if(this.m_image == null)
return;
//计算放大的范围和画图的坐标
int pbh = this.pictureBox1.Height;
int pbw = this.pictureBox1.Width;
int newht = (int)(this.m_curDrawRect.Height / 1.2);
int newwt = (int)(this.m_curDrawRect.Width / 1.2);
int ox = (int)((this.m_curDrawRect.Width - newwt)/2);
int oy = (int)((this.m_curDrawRect.Height - newht)/2);
int x = this.m_curDrawRect.X + ox;
int y = this.m_curDrawRect.Y + oy; //判断边缘
if(newwt>pbw)
{
if(x>0) x=0;
if((x+newwt)<pbw) x=pbw-newwt;
}
else
{
x = (int)((pbw-newwt)/2);
} if(newht>pbh)
{
if(y>0) y=0;
if((y+newht)<pbh) y=pbh-newht;
}
else
{
y = (int)((pbh-newht)/2);
} //如果缩小小于图片框大小的倍数则放弃操作
if((this.pictureBox1.Height/newht)>5||(this.pictureBox1.Width/newwt)>5)
return;
this.m_curDrawRect = new Rectangle(x,y,newwt,newht);
this.pictureBox1.Invalidate();
}
/// 全图显示
/// </summary>
public void Full()
{
if (this.m_image == null)
return; int pbh = this.pictureBox1.Height;
int pbw = this.pictureBox1.Width; int ht = this.m_image.Height;
int wt = this.m_image.Width; int newht = pbh;
int newwt = pbw;
int x = 0;
int y = 0; //计算画图坐标和显示区域大小
if (ht < pbh && wt < pbw)//图片高宽均小于图片框
{
newht = ht;
newwt = wt;
x = (pbw - wt) / 2;
y = (pbh - ht) / 2;
}
else if (ht > pbh && wt <= pbw)//图片高>图片框高,宽<=图片框宽
{
newht = pbh;
newwt = (int)((pbh / (ht * 1.0)) * wt);
x = (pbw - newwt) / 2;
y = 0;
}
else if (ht <= pbh && wt > pbw)//反过来
{
newht = (int)((pbw / (wt * 1.0)) * ht);
newwt = pbw;
x = 0;
y = (pbh - newht) / 2;
}
else//图片高宽均大于图片框
{
if ((ht / (pbh * 1.0)) > (wt / (pbw * 1.0)))//同比高大于宽
{
newht = pbh;
newwt = (int)((pbh / (ht * 1.0)) * wt);
x = (pbw - newwt) / 2;
y = 0; }
else//同比宽大于高
{
newht = (int)((pbw / (wt * 1.0)) * ht);
newwt = pbw;
x = 0;
y = (pbh - newht) / 2;
}
} this.m_curDrawRect = new Rectangle(x, y, newwt, newht);
this.m_curImgRect = new Rectangle(0, 0, wt, ht);
this.pictureBox1.Invalidate();
} /// <summary>
/// 原图大小显示
/// </summary>
public void Zoom()
{
if (this.m_image == null)
return; int pbh = this.pictureBox1.Height;
int pbw = this.pictureBox1.Width; int ht = this.m_image.Height;
int wt = this.m_image.Width; int newht = pbh;
int newwt = pbw;
int x = 0;
int y = 0; //计算画图坐标和显示区域大小
if (ht < pbh && wt < pbw)//图片高宽均小于图片框
{
newht = ht;
newwt = wt;
x = (pbw - wt) / 2;
y = (pbh - ht) / 2;
}
else if (ht > pbh && wt <= pbw)//图片高>图片框高,宽<=图片框宽
{
x = (pbw - wt) / 2;
y = 0 - (ht - pbh) / 2;
newht = ht;
newwt = wt;
}
else if (ht <= pbh && wt > pbw)//反过来
{
x = 0 - (wt - pbw) / 2;
y = (pbh - ht) / 2;
newht = ht;
newwt = wt;
}
else//图片高宽均大于图片框
{
x = 0 - (wt - pbw) / 2;
y = 0 - (ht - pbh) / 2;
newht = ht;
newwt = wt;
} this.m_curDrawRect = new Rectangle(x, y, newwt, newht);
this.m_curImgRect = new Rectangle(0, 0, wt, ht);
this.pictureBox1.Invalidate();
} #endregion #region 放大缩小平移具体实现函数 /// <summary>
/// 拉框放大
/// </summary>
private void AutoZoomIn()
{
if (this.m_image == null)
return; //picturebox控件范围
double pbh = this.pictureBox1.Height;
double pbw = this.pictureBox1.Width; //图片实际范围
double drawh = this.m_curDrawRect.Height;
double draww = this.m_curDrawRect.Width;
double drawX = this.m_curDrawRect.X;
double drawY = this.m_curDrawRect.Y; //图片原始范围
double imgH = this.m_image.Height;
double imgW = this.m_image.Width; //zoom框范围
double zoomh = this.m_autoZoom.Height;
double zoomw = this.m_autoZoom.Width;
double zoomX = this.m_autoZoom.X;
double zoomY = this.m_autoZoom.Y; //Zoom矩形不能是一条线或点,也就是width和height要大于0
if (zoomh <= 0 || zoomw <= 0) return;
//zoom框宽高比
double zoomWHScale = (double)zoomw / (double)zoomh;
//picturebox宽高比
double picboxWHScale = (double)pbw / (double)pbh; //把zoom框以picturebox的范围当做最大范围,进行缩放
double fillZoomX = 0, fillZoomY = 0, fillZoomW = 0, fillZoomH = 0;
if (zoomWHScale > picboxWHScale)
{
fillZoomX = 0;
fillZoomW = pbw;
double zoomInScale = fillZoomW / zoomw;
fillZoomH = zoomh * zoomInScale;
fillZoomY = (pbh - fillZoomH) / 2;
}
else
{
fillZoomY = 0;
fillZoomH = pbh;
double zoomInScale = fillZoomH / zoomh;
fillZoomW = zoomw * zoomInScale;
fillZoomX = (pbw - fillZoomW) / 2;
} //zoom框与图片实际显示范围的交集
Rectangle oldZoomDraw = Rectangle.Intersect(this.m_autoZoom, this.m_curDrawRect); //矩形交集为空时跳出
if (oldZoomDraw.Width <= 0 || oldZoomDraw.Height <= 0 || oldZoomDraw.X > pbw || oldZoomDraw.Y > pbh) return; //缩放后的zoom框与zoom框的宽高比
double zoomXScale = fillZoomW / zoomw;
double zoomYScale = fillZoomH / zoomh; //缩放后的zoom框中的图片实际显示范围
double newZoomDrawX = (oldZoomDraw.X - zoomX) * zoomXScale + fillZoomX;
double newZoomDrawY = (oldZoomDraw.Y - zoomY) * zoomYScale + fillZoomY;
double newZoomDrawW = oldZoomDraw.Width * zoomXScale;
double newZoomDrawH = oldZoomDraw.Height * zoomYScale; //图片实际显示与图片原始的宽高比
double oldZoomImgXScale = draww / imgW;
double oldZoomImgYScale = drawh / imgH; //zoom框中图片的范围(以图片原始大小为单位)
double ZoomImgX = (oldZoomDraw.X - drawX) / oldZoomImgXScale;
double ZoomImgY = (oldZoomDraw.Y - drawY) / oldZoomImgYScale;
double ZoomImgW = oldZoomDraw.Width / oldZoomImgXScale;
double ZoomImgH = oldZoomDraw.Height / oldZoomImgYScale; //缩放后图片实际显示与图片原始的宽高比
double newZoomImgXScale = newZoomDrawW / ZoomImgW;
double newZoomImgYScale = newZoomDrawH / ZoomImgH; //最终图片的实际显示范围
double newDrawX = newZoomDrawX - ZoomImgX * newZoomImgXScale;
double newDrawY = newZoomDrawY - ZoomImgY * newZoomImgYScale;
double newDrawW = imgW * newZoomImgXScale;
double newDrawH = imgH * newZoomImgYScale; //如果缩小小于图片框大小的倍数则放弃操作
if (((double)this.pictureBox1.Height / newDrawH) < 0.1 || ((double)this.pictureBox1.Width / newDrawW) < 0.1) return; this.m_curDrawRect = new Rectangle((int)Math.Truncate(newDrawX), (int)Math.Truncate(newDrawY), (int)Math.Truncate(newDrawW), (int)Math.Truncate(newDrawH));
this.pictureBox1.Invalidate();
}
{
//非左键不操作
if(e.Button != MouseButtons.Left) return; if (this._actionType == picViewerActionType.ZoomIn || this._actionType == picViewerActionType.ZoomOut)
{
if (this.m_autoZoom == Rectangle.Empty)
{
//判断鼠标是否落在图片绘图区
if ((this._actionType == picViewerActionType.ZoomIn && this.m_curDrawRect.Contains(e.X, e.Y)) ||
(this._actionType == picViewerActionType.ZoomOut && e.X <= this.pictureBox1.Width && e.Y <= this.pictureBox1.Height))
{
this.m_padPoint = Point.Empty;
this.m_zoomPoint = new Point(e.X, e.Y);
}
else
{
this.m_padPoint = Point.Empty;
this.m_zoomPoint = Point.Empty;
}
}
}
else if (this._actionType == picViewerActionType.Pan)
{
this.m_padPoint = new Point(e.X, e.Y);
this.m_zoomPoint = Point.Empty;
Cursor.Current = this.m_paning;
}
} private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (this._actionType == picViewerActionType.ZoomIn || this._actionType == picViewerActionType.ZoomOut)
{
if (this.m_zoomPoint != Point.Empty)//拉框放大,画虚线框
{
ControlPaint.DrawReversibleFrame(m_TrackRectangle, this.BackColor, FrameStyle.Dashed); //鼠标不能超过的范围
Rectangle standardRect;
if (this._actionType == picViewerActionType.ZoomIn)
standardRect = this.m_curDrawRect;
else
standardRect = new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height); Point tmpPoint = new Point(e.X, e.Y);
if (!standardRect.Contains(tmpPoint))
{
if (tmpPoint.X < standardRect.Left)
tmpPoint.X = standardRect.Left;
if (tmpPoint.X > standardRect.Right)
tmpPoint.X = standardRect.Right;
if (tmpPoint.Y < standardRect.Top)
tmpPoint.Y = standardRect.Top;
if (tmpPoint.Y > standardRect.Bottom)
tmpPoint.Y = standardRect.Bottom;
} Point startPoint = this.PointToScreen(this.m_zoomPoint);
Point endPoint = this.PointToScreen(tmpPoint);
int width = endPoint.X - startPoint.X;
int height = endPoint.Y - startPoint.Y;
m_TrackRectangle = new Rectangle(startPoint.X, startPoint.Y, width, height); ControlPaint.DrawReversibleFrame(m_TrackRectangle, this.BackColor, FrameStyle.Dashed);
}
}
else if (this._actionType == picViewerActionType.Pan)
{
Point thisPoint = new Point(e.X, e.Y);
Rectangle standardRect = new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
//超出picturebox就不处理
if (this.m_padPoint != Point.Empty && standardRect.Contains(thisPoint))//平移
{
this.Pad(thisPoint);
this.m_padPoint = thisPoint;
}
}
} private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button != MouseButtons.Left) return; Point toPoint = new Point(e.X, e.Y);
if (this._actionType == picViewerActionType.ZoomIn || this._actionType == picViewerActionType.ZoomOut)
{
if (this.m_zoomPoint != Point.Empty)//拉框放大
{
//鼠标不能超过的范围
Rectangle standardRect;
if (this._actionType == picViewerActionType.ZoomIn)
standardRect = this.m_curDrawRect;
else
standardRect = new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height); Point tmpPoint = new Point(e.X, e.Y);
if (!standardRect.Contains(tmpPoint))
{
if (tmpPoint.X < standardRect.Left)
tmpPoint.X = standardRect.Left;
if (tmpPoint.X > standardRect.Right)
tmpPoint.X = standardRect.Right;
if (tmpPoint.Y < standardRect.Top)
tmpPoint.Y = standardRect.Top;
if (tmpPoint.Y > standardRect.Bottom)
tmpPoint.Y = standardRect.Bottom;
} int x = this.m_zoomPoint.X;
int y = this.m_zoomPoint.Y;
int width = Math.Abs(toPoint.X - x);
int height = Math.Abs(toPoint.Y - y); if (x > toPoint.X) x = toPoint.X;
if (y > toPoint.Y) y = toPoint.Y;
this.m_autoZoom = new Rectangle(x, y, width, height); this.m_zoomPoint = Point.Empty; //取消掉图片上的临时框
ControlPaint.DrawReversibleFrame(m_TrackRectangle, this.BackColor, FrameStyle.Dashed);
m_TrackRectangle = new Rectangle(0, 0, 0, 0);
///放大和缩小
if (this._actionType == picViewerActionType.ZoomIn)
this.AutoZoomIn();
else if (this._actionType == picViewerActionType.ZoomOut)
this.AutoZoomOut();
this.m_autoZoom = Rectangle.Empty;
}
}
else if (this._actionType == picViewerActionType.Pan)
{
if (this.m_padPoint != Point.Empty)//平移
{
this.m_padPoint = Point.Empty;
} this.Cursor = this.m_pan;
}
} #endregion private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//无图片时不执行操作
if(this.m_image == null)
return;
//初始化显示图片
if(this.m_curDrawRect == Rectangle.Empty || this.m_curImgRect == Rectangle.Empty)
{
this.Full();
return;
} try
{
//判断拉框放大的框是否还存在
if (this.m_autoZoom != Rectangle.Empty)
{
ControlPaint.DrawReversibleFrame(m_TrackRectangle, this.BackColor, FrameStyle.Dashed);
m_TrackRectangle = new Rectangle(0, 0, 0, 0);
this.m_autoZoom = Rectangle.Empty;
} //设置高质量,低速度呈现平滑程度
Graphics grap = e.Graphics;
// grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// grap.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
//清空一下画布
grap.Clear(m_backColor);
grap.DrawImage(this.m_image, this.m_curDrawRect, this.m_curImgRect, GraphicsUnit.Pixel);
}
catch (Exception ex)
{ } base.OnPaint(e);
}
public void refresh()
{
SetStyle(ControlStyles.SupportsTransparentBackColor,true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
Refresh();
}
}
}
this.picViewer1.ActionType = picViewerActionType.ZoomOut;
this.picViewer1.ActionType = picViewerActionType.Pan;
控制鼠标操作是平移还是放大还是缩小
this.AutoZoomOut()這两个函数呀,方便提供上来吗?
/// 拉框缩小
/// </summary>
private void AutoZoomOut()
{
if (this.m_image == null)
return; //各个高宽取值
double pbh = this.pictureBox1.Height;
double pbw = this.pictureBox1.Width; double drawh = this.m_curDrawRect.Height;
double draww = this.m_curDrawRect.Width;
double drawX = this.m_curDrawRect.X;
double drawY = this.m_curDrawRect.Y; double imgH = this.m_image.Height;
double imgW = this.m_image.Width; double zoomh = this.m_autoZoom.Height;
double zoomw = this.m_autoZoom.Width;
double zoomX = this.m_autoZoom.X;
double zoomY = this.m_autoZoom.Y; //Zoom矩形不能是一条线或点,也就是width和height要大于0
if (zoomh <= 0 || zoomw <= 0) return; double zoomWHScale = (double)zoomw / (double)zoomh;
double picboxWHScale = (double)pbw / (double)pbh; double fillZoomX = 0, fillZoomY = 0, fillZoomW = 0, fillZoomH = 0; if (zoomWHScale > picboxWHScale)
{
fillZoomX = 0;
fillZoomW = pbw;
double zoomInScale = fillZoomW / zoomw;
fillZoomH = zoomh * zoomInScale;
fillZoomY = (pbh - fillZoomH) / 2;
}
else
{
fillZoomY = 0;
fillZoomH = pbh;
double zoomInScale = fillZoomH / zoomh;
fillZoomW = zoomw * zoomInScale;
fillZoomX = (pbw - fillZoomW) / 2;
} Rectangle fillZoom = new Rectangle((int)Math.Truncate(fillZoomX), (int)Math.Truncate(fillZoomY), (int)Math.Truncate(fillZoomW), (int)Math.Truncate(fillZoomH));
Rectangle oldZoomDraw = Rectangle.Intersect(fillZoom, this.m_curDrawRect); //矩形交集为空时跳出
if (oldZoomDraw.Width <= 0 || oldZoomDraw.Height <= 0 || oldZoomDraw.X > pbw || oldZoomDraw.Y > pbh) return;
double zoomXScale = fillZoomW / zoomw;
double zoomYScale = fillZoomH / zoomh;
double newZoomDrawX = (oldZoomDraw.X - fillZoomX) / zoomXScale + zoomX;
double newZoomDrawY = (oldZoomDraw.Y - fillZoomY) / zoomYScale + zoomY;
double newZoomDrawW = oldZoomDraw.Width / zoomXScale;
double newZoomDrawH = oldZoomDraw.Height / zoomYScale; double oldZoomImgXScale = draww / imgW;
double oldZoomImgYScale = drawh / imgH; double ZoomImgX = (oldZoomDraw.X - drawX) / oldZoomImgXScale;
double ZoomImgY = (oldZoomDraw.Y - drawY) / oldZoomImgYScale;
double ZoomImgW = oldZoomDraw.Width / oldZoomImgXScale;
double ZoomImgH = oldZoomDraw.Height / oldZoomImgYScale; double newZoomImgXScale = newZoomDrawW / ZoomImgW;
double newZoomImgYScale = newZoomDrawH / ZoomImgH; double newDrawX = newZoomDrawX - ZoomImgX * newZoomImgXScale;
double newDrawY = newZoomDrawY - ZoomImgY * newZoomImgYScale;
double newDrawW = imgW * newZoomImgXScale;
double newDrawH = imgH * newZoomImgYScale; //如果缩小小于图片框大小的倍数则放弃操作
if (((double)this.pictureBox1.Height / newDrawH) > 10 || ((double)this.pictureBox1.Width / newDrawW) > 10) return; this.m_curDrawRect = new Rectangle((int)Math.Truncate(newDrawX), (int)Math.Truncate(newDrawY), (int)Math.Truncate(newDrawW), (int)Math.Truncate(newDrawH)); this.pictureBox1.Invalidate();
} /// <summary>
/// 平移
/// </summary>
/// <param name="toPoint"></param>
private void Pad(Point toPoint)
{
int pbh = this.pictureBox1.Height;
int pbw = this.pictureBox1.Width; int newht = this.m_curDrawRect.Height;
int newwt = this.m_curDrawRect.Width;
int x = this.m_curDrawRect.X + (toPoint.X - this.m_padPoint.X);
int y = this.m_curDrawRect.Y + (toPoint.Y - this.m_padPoint.Y); if(this.m_curDrawRect.X == x && this.m_curDrawRect.Y == y)
return; Rectangle newDrawRect = new Rectangle(x, y, newwt, newht); //如果图片完全不在picturebox中则不处理
Rectangle picBoxRect = new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
if (newDrawRect.IntersectsWith(picBoxRect) == false) return; this.m_curDrawRect = newDrawRect;
this.pictureBox1.Invalidate();
}