C# 图片缩放,拖拽后保存成图片的功能
窗体界面部分如下:
鼠标的缩放功能需要手动在 OpertaionImg.Designer.cs 文件里面添加一句代码,具体代码如下:
1 //picturePhoto显示图片的控件
2 this.picturePhoto.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.picturePhoto_MouseWheel);
下面是窗体的后台代码:
1 public partial class OpertaionImg : Form 2 { 3 Bitmap myBmp; 4 Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置 5 bool isMove = false; //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下) 6 int zoomStep = 20; //缩放步长 7 public OpertaionImg() 8 { 9 InitializeComponent(); 10 } 11 12 private void btnChoise_Click(object sender, EventArgs e) 13 { 14 string filename = ""; 15 OpenFileDialog dlg = new OpenFileDialog(); 16 dlg.Filter = "Tiff文件|*.tif|Bmp文件|*.bmp|Erdas img文件|*.img|EVNI文件|*.hdr|jpeg文件|*.jpg|raw文件|*.raw|vrt文件|*.vrt|所有文件|*.*"; 17 dlg.FilterIndex = 8; 18 if (dlg.ShowDialog() == DialogResult.OK) 19 { 20 filename = dlg.FileName; 21 } 22 if (filename == "") 23 { 24 return; 25 } 26 myBmp = new Bitmap(filename); 27 if (myBmp == null) 28 { 29 MessageBox.Show("读取失败"); 30 return; 31 } 32 // textBox1.Text = filename; 33 picturePhoto.Image = myBmp; 34 picturePhoto.SizeMode = PictureBoxSizeMode.Zoom; //设置picturebox为缩放模式 35 picturePhoto.Width = myBmp.Width; 36 picturePhoto.Height = myBmp.Height; 37 } 38 39 private void picturePhoto_MouseDown(object sender, MouseEventArgs e) 40 { 41 if (e.Button == MouseButtons.Left) 42 { 43 mouseDownPoint.X = Cursor.Position.X; 44 mouseDownPoint.Y = Cursor.Position.Y; 45 isMove = true; 46 picturePhoto.Focus(); 47 } 48 } 49 50 private void picturePhoto_MouseUp(object sender, MouseEventArgs e) 51 { 52 if (e.Button == MouseButtons.Left) 53 { 54 isMove = false; 55 } 56 } 57 58 private void picturePhoto_MouseMove(object sender, MouseEventArgs e) 59 { 60 picturePhoto.Focus(); 61 if (isMove) 62 { 63 int x, y; 64 int moveX, moveY; 65 moveX = Cursor.Position.X - mouseDownPoint.X; 66 moveY = Cursor.Position.Y - mouseDownPoint.Y; 67 x = picturePhoto.Location.X + moveX; 68 y = picturePhoto.Location.Y + moveY; 69 picturePhoto.Location = new Point(x, y); 70 mouseDownPoint.X = Cursor.Position.X; 71 mouseDownPoint.Y = Cursor.Position.Y; 72 } 73 } 74 private void picturePhoto_MouseWheel(object sender, MouseEventArgs e) 75 { 76 int x = e.Location.X; 77 int y = e.Location.Y; 78 int ow = picturePhoto.Width; 79 int oh = picturePhoto.Height; 80 int VX, VY; 81 if (e.Delta > 0) 82 { 83 picturePhoto.Width += zoomStep; 84 picturePhoto.Height += zoomStep; 85 86 PropertyInfo pInfo = picturePhoto.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic); 87 Rectangle rect = (Rectangle)pInfo.GetValue(picturePhoto, null); 88 89 picturePhoto.Width = rect.Width; 90 picturePhoto.Height = rect.Height; 91 } 92 if (e.Delta < 0) 93 { 94 if (picturePhoto.Width < myBmp.Width / 10) 95 return; 96 picturePhoto.Width -= zoomStep; 97 picturePhoto.Height -= zoomStep; 98 PropertyInfo pInfo = picturePhoto.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic); 99 Rectangle rect = (Rectangle)pInfo.GetValue(picturePhoto, null); 100 picturePhoto.Width = rect.Width; 101 picturePhoto.Height = rect.Height; 102 } 103 104 VX = (int)((double)x * (ow - picturePhoto.Width) / ow); 105 VY = (int)((double)y * (oh - picturePhoto.Height) / oh); 106 picturePhoto.Location = new Point(picturePhoto.Location.X + VX, picturePhoto.Location.Y + VY); 107 } 108 109 private void btnSave_Click(object sender, EventArgs e) 110 { 111 SaveFileDialog sa = new SaveFileDialog(); 112 //设置文件类型 113 sa.Filter = "图片文件(*.png)|*.png"; 114 //设置默认文件类型显示顺序 115 sa.FilterIndex = 1; 116 if (sa.ShowDialog() == DialogResult.OK) 117 { 118 Bitmap bit = new Bitmap(picturePhoto.Width, picturePhoto.Height);//实例化一个和窗体一样大的bitmap 119 Graphics g = Graphics.FromImage(bit); 120 g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高 121 // g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(panel2.Width, panel2.Height));//保存整个窗体为图片 122 g.CopyFromScreen(picturePhoto.PointToScreen(Point.Empty), Point.Empty, picturePhoto.Size);//只保存某个控件(这里是panel游戏区) 123 bit.Save(sa.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好 124 } 125 } 126 }