PictureBox缩放模式效果与图形对象

时间:2013-11-20 16:49:04

标签: c# .net visual-studio-2010 graphics picturebox

当您将图像加载到PictureBox中时,图像放置会有缩放,如何使用图形对象实现相同的效果?

1 个答案:

答案 0 :(得分:3)

我认为您的意思是,您希望自己在<{1}} 中使用<{1}}并使用Image对象作为Rectangle渲染其图像处于Graphics模式。请尝试以下代码。我想你想在表单上绘制PictureBox,绘图代码应该添加到表单的Zoom事件处理程序中:

Image

要在您的表单上完美地测试它,您还必须设置Paint并启用//The Rectangle (corresponds to the PictureBox.ClientRectangle) //we use here is the Form.ClientRectangle //Here is the Paint event handler of your Form1 private void Form1_Paint(object sender, EventArgs e){ ZoomDrawImage(e.Graphics, yourImage, ClientRectangle); } //use this method to draw the image like as the zooming feature of PictureBox private void ZoomDrawImage(Graphics g, Image img, Rectangle bounds){ decimal r1 = (decimal) img.Width/img.Height; decimal r2 = (decimal) bounds.Width/bounds.Height; int w = bounds.Width; int h = bounds.Height; if(r1 > r2){ w = bounds.Width; h = (int) (w / r1); } else if(r1 < r2){ h = bounds.Height; w = (int) (r1 * h); } int x = (bounds.Width - w)/2; int y = (bounds.Height - h)/2; g.DrawImage(img, new Rectangle(x,y,w,h)); }

ResizeRedraw = true
相关问题