C# 图片缩放,拖拽后保存成图片的功能

时间:2023-03-10 04:52:55
C# 图片缩放,拖拽后保存成图片的功能

窗体界面部分如下:

C# 图片缩放,拖拽后保存成图片的功能

鼠标的缩放功能需要手动在 OpertaionImg.Designer.cs 文件里面添加一句代码,具体代码如下:

//picturePhoto显示图片的控件

this.picturePhoto.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.picturePhoto_MouseWheel);

下面是窗体的后台代码:

 public partial class OpertaionImg : Form
{
Bitmap myBmp;
Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置
bool isMove = false; //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下)
int zoomStep = ; //缩放步长
public OpertaionImg()
{
InitializeComponent();
} private void btnChoise_Click(object sender, EventArgs e)
{
string filename = "";
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Tiff文件|*.tif|Bmp文件|*.bmp|Erdas img文件|*.img|EVNI文件|*.hdr|jpeg文件|*.jpg|raw文件|*.raw|vrt文件|*.vrt|所有文件|*.*";
dlg.FilterIndex = ;
if (dlg.ShowDialog() == DialogResult.OK)
{
filename = dlg.FileName;
}
if (filename == "")
{
return;
}
myBmp = new Bitmap(filename);
if (myBmp == null)
{
MessageBox.Show("读取失败");
return;
}
// textBox1.Text = filename;
picturePhoto.Image = myBmp;
picturePhoto.SizeMode = PictureBoxSizeMode.Zoom; //设置picturebox为缩放模式
picturePhoto.Width = myBmp.Width;
picturePhoto.Height = myBmp.Height;
} private void picturePhoto_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
isMove = true;
picturePhoto.Focus();
}
} private void picturePhoto_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMove = false;
}
} private void picturePhoto_MouseMove(object sender, MouseEventArgs e)
{
picturePhoto.Focus();
if (isMove)
{
int x, y;
int moveX, moveY;
moveX = Cursor.Position.X - mouseDownPoint.X;
moveY = Cursor.Position.Y - mouseDownPoint.Y;
x = picturePhoto.Location.X + moveX;
y = picturePhoto.Location.Y + moveY;
picturePhoto.Location = new Point(x, y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
private void picturePhoto_MouseWheel(object sender, MouseEventArgs e)
{
int x = e.Location.X;
int y = e.Location.Y;
int ow = picturePhoto.Width;
int oh = picturePhoto.Height;
int VX, VY;
if (e.Delta > )
{
picturePhoto.Width += zoomStep;
picturePhoto.Height += zoomStep; PropertyInfo pInfo = picturePhoto.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(picturePhoto, null); picturePhoto.Width = rect.Width;
picturePhoto.Height = rect.Height;
}
if (e.Delta < )
{
if (picturePhoto.Width < myBmp.Width / )
return;
picturePhoto.Width -= zoomStep;
picturePhoto.Height -= zoomStep;
PropertyInfo pInfo = picturePhoto.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(picturePhoto, null);
picturePhoto.Width = rect.Width;
picturePhoto.Height = rect.Height;
} VX = (int)((double)x * (ow - picturePhoto.Width) / ow);
VY = (int)((double)y * (oh - picturePhoto.Height) / oh);
picturePhoto.Location = new Point(picturePhoto.Location.X + VX, picturePhoto.Location.Y + VY);
} private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sa = new SaveFileDialog();
//设置文件类型
sa.Filter = "图片文件(*.png)|*.png";
//设置默认文件类型显示顺序
sa.FilterIndex = ;
if (sa.ShowDialog() == DialogResult.OK)
{
Bitmap bit = new Bitmap(picturePhoto.Width, picturePhoto.Height);//实例化一个和窗体一样大的bitmap
Graphics g = Graphics.FromImage(bit);
g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
// g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(panel2.Width, panel2.Height));//保存整个窗体为图片
g.CopyFromScreen(picturePhoto.PointToScreen(Point.Empty), Point.Empty, picturePhoto.Size);//只保存某个控件(这里是panel游戏区)
bit.Save(sa.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
}
}
}