Picturebox实现图片的缩放

程序中要弄个简单的图片查看器,可以按比例缩放大小的,当然可以调用windows的图片查看器,不过想想还是自己动手弄个简单的吧。。

 

 

缩放操作在Picturebox重绘的时候触发执行。如下

 

代码
1 //重绘处理部分
2   private void pipeImagePictureBox_Paint(object sender, PaintEventArgs e)
3 {
4 try
5 {
6 if (pipeImagePath != "")
7 {
8 pipeImage = new Bitmap(pipeImagePath);
9 }
10 Graphics g = e.Graphics;
11 //设置高质量插值法
12   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
13 //设置高质量,低速度呈现平滑程度
14   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
15 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
16 //消除锯齿
17   g.SmoothingMode = SmoothingMode.AntiAlias;
18
19 float fx = (float)(this.pipeImageZoomNumericUpDown.Value / 100);
20 float fy = (float)(this.pipeImageZoomNumericUpDown.Value / 100);
21 int w = (int)(pipeImage.Width * fx), h = (int)(pipeImage.Height * fy);
22 pipeImagePictureBox.Width = w;
23 pipeImagePictureBox.Height = h;
24 int W = (int)(pipeImagePanel.Width), H = (int)(pipeImagePanel.Height);
25 pipeImagePictureBox.Location = new System.Drawing.Point((W - w) / 2, (H - h) / 2);
26 pipeImagePictureBox.Size = new Size(w,h);
27
28 Rectangle newRectangle = new Rectangle(0, 0, w, h);
29 g.DrawImage(pipeImage, newRectangle);
30 }
31 catch (Exception ex)
32 {
33 //pipeImagePictureBox.Image = null;
34   }
35 }

 

 

当然,除了缩放还有其他的要做。。

posted on 2010-11-09 18:18  小交响曲  阅读(1197)  评论(0编辑  收藏  举报

导航