近期写程序需要一个图片放大、缩小和移动的功能,上网查找参考了一些热心网友的资料,再结合自己的实际需要,写了一个class,用于直接在新窗口中显示、放大、缩小、保存图片的功能,这样的话以后使用的时候可以直接调用,代码记录如下:
引用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; using System.IO;
其余代码:
/// <summary>
/// 用于显示图片,提供放大、缩小、移动、保存图片的功能
/// </summary>
class FK_ShowImg
{
Form F = new Form();
PictureBox pictureBox = new PictureBox();
int xPos;
int yPos;
bool MoveFlag;
public void ShowImg(Bitmap bmp)
{
#region 定义FORM的基本属性
F.Show();
F.Text = "显示图片";
F.Width = 1024;
F.Height = 768;
#endregion
#region 创建菜单
MenuStrip MS = new MenuStrip();
ToolStripMenuItem tsmi = new ToolStripMenuItem("文件");
//tsmi.Click += DemoClick;
ToolStripMenuItem tsmi2 = new ToolStripMenuItem("图片另存为");
tsmi2.Click += saveIMG;
tsmi.DropDownItems.Add(tsmi2);
MS.Items.Add(tsmi);
F.Controls.Add(MS);
#endregion
#region 显示图片
pictureBox.Image = bmp;
pictureBox.MouseWheel += F_MouseWheel;
F.MouseWheel += F_MouseWheel;
pictureBox.MouseMove += F_MouseMove;
pictureBox.MouseDown += F_MouseDown;
pictureBox.MouseUp += F_MouseUp;
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox.Width = 800;
pictureBox.Height = 600;
//图片居中
pictureBox.Top = (F.ClientRectangle.Height - pictureBox.Height) / 2;
pictureBox.Left = (F.ClientRectangle.Width - pictureBox.Width) / 2;
F.Controls.Add(pictureBox);
#endregion
}
PRivate void F_MouseUp(object sender, MouseEventArgs e)
{
//throw new NotImplementedException();
//鼠标已经抬起
MoveFlag = false;
}
private void F_MouseDown(object sender, MouseEventArgs e)
{
//throw new NotImplementedException();
this.pictureBox.Focus();
MoveFlag = true;//已经按下.
xPos = e.X;//当前x坐标.
yPos = e.Y;//当前y坐标.
}
private void F_MouseMove(object sender, MouseEventArgs e)
{
//throw new NotImplementedException();
//只在鼠标按下时绘制移动
if (MoveFlag)
{
pictureBox.Left += Convert.ToInt16(e.X - xPos);//设置x坐标.
pictureBox.Top += Convert.ToInt16(e.Y - yPos);//设置y坐标.
}
}
//自己定义个保存图片的动作
private void saveIMG(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Jpg 图片|*.jpg|Bmp 图片|*.bmp|Gif 图片|*.gif|Png 图片|*.png|Wmf 图片|*.wmf";
saveFileDialog.FilterIndex = 0;
if (pictureBox.Image == null)
{
MessageBox.Show("没有预览图片!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (pictureBox.Image != null)
{
pictureBox.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
private void F_MouseWheel(object sender, MouseEventArgs e)
{
//判断上滑还是下滑
if (e.Delta < 0)
{
//计算缩放大小
this.pictureBox.Width = this.pictureBox.Width * 9 / 10;
this.pictureBox.Height = this.pictureBox.Height * 9 / 10;
}
else
{
this.pictureBox.Width = this.pictureBox.Width * 11 / 10;
this.pictureBox.Height = this.pictureBox.Height * 11 / 10;
}
}
}主程序中直接使用时:
FK_ShowImg F = new FK_ShowImg(); Bitmap bmp = (Bitmap)Bitmap.FromFile(@"D:\temp\test.png"); F.ShowImg(bmp);
结果显示如下:

