Picturebox实现图片的缩放

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

 

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

//重绘处理部分 private void pipeImagePictureBox_Paint(object sender, PaintEventArgs e) { try { if (pipeImagePath != "") { pipeImage = new Bitmap(pipeImagePath); } Graphics g = e.Graphics; //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //消除锯齿 g.SmoothingMode = SmoothingMode.AntiAlias; float fx = (float)(this.pipeImageZoomNumericUpDown.Value / 100); float fy = (float)(this.pipeImageZoomNumericUpDown.Value / 100); int w = (int)(pipeImage.Width * fx), h = (int)(pipeImage.Height * fy); pipeImagePictureBox.Width = w; pipeImagePictureBox.Height = h; int W = (int)(pipeImagePanel.Width), H = (int)(pipeImagePanel.Height); pipeImagePictureBox.Location = new System.Drawing.Point((W - w) / 2, (H - h) / 2); pipeImagePictureBox.Size = new Size(w,h); Rectangle newRectangle = new Rectangle(0, 0, w, h); g.DrawImage(pipeImage, newRectangle); } catch (Exception ex) { //pipeImagePictureBox.Image = null; } }

 

 

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

你可能感兴趣的:(exception,windows,object,null,float)