Winform 图片鼠标滚动查看(放大,缩小,旋转,拖动查看)[日常随笔]

方法千千万,我只是其中一笔[通过控制PictureBox来控制图片,图片完全施展在控件中]...几久不做,还真有点陌生!

窗体构造中添加鼠标滚动:

 /// <summary>
/// 窗体构造方法
/// </summary>
public CandidateForm()
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(CandidateForm_MouseWheel);
}

滚动监听:并且保持图片正中

 /// <summary>
/// 鼠标滚动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CandidateForm_MouseWheel(object sender, MouseEventArgs e)
{
this.pic.Dock = DockStyle.None;
this.pic.BorderStyle = BorderStyle.FixedSingle;
Size size = this.pic.Size;
size.Width += e.Delta;
if (size.Width > pic.Image.Width)
{
pic.Width = pic.Image.Width;
pic.Height = pic.Image.Height;
}
else if (size.Width * pic.Image.Height / pic.Image.Width < pic.Parent.Height - )
{
return;
}
else
{
pic.Width = size.Width;
pic.Height = size.Width * pic.Image.Height / pic.Image.Width;
}
pic.Left = (pic.Parent.Width - pic.Width) / ;
pic.Top = (pic.Parent.Height - pic.Height) / ;
}

旋转:

 private void 左旋ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (basicBt != null)
{
basicBt = Tools.Rotate(basicBt, );
height = this.pic.Width;
width = this.pic.Height;
setBasicPb(basicBt);
}
} private void 右旋ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (basicBt != null)
{
basicBt = Tools.Rotate(basicBt, );
height = this.pic.Width;
width = this.pic.Height;
setBasicPb(basicBt);
}
}

拖动直接在鼠标点击事件中,对PictureBox位置根据鼠标拖动进行处理就好.关键代码:

 //拖动
if (canDrag)
{
pic.Location =
new Point(pic.Left + e.X - p0.X, pic.Top + e.Y - p0.Y);
}

如果不是特意做图片查看,一般还会有对图片进行处理的功能,及需要用到鼠标拖动啊,针对原图片的定位等等~~ 则要用到图片的相对位置定位.

因为使用的是通过改变PictureBox的方法进行处理,所以只需算出比例即可.

    rate = (double)pic.Width / (double)pic.Image.Width;
上一篇:MySQL 存储过程探秘


下一篇:深圳尚学堂:Swift中的“!”和“?”