使用图形对象的PictureBox缩放模式效果 [英] PictureBox Zoom Mode Effect With Graphics Object

查看:189
本文介绍了使用图形对象的PictureBox缩放模式效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将图片加载到图片框中时,图片放置位置会缩放,我如何通过图片对象实现同样的效果?

解决方案 a Rectangle 并使用一些 Graphics 对象,如 PictureBox 将其图像渲染到 Zoom 模式。尝试下面的代码。我想你想在一个Form上绘制 Image ,绘图代码应该添加到 Paint 事件处理程序中你的表单:

  //矩形(对应于PictureBox.ClientRectangle)
//我们在这里使用的是Form.ClientRectangle
//这里是Form1的Paint事件处理程序
private void Form1_Paint(object sender,EventArgs e){
ZoomDrawImage(e.Graphics,yourImage,ClientRectangle);
}
//使用此方法绘制图像,如同PictureBox的缩放功能一样
private void ZoomDrawImage(Graphics g,Image img,Rectangle bounds){
decimal r1 = (十进制)img.Width / img.Height;
十进制r2 =(十进制)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 h = bounds.Height;
w =(int)(r1 * h);
}
int x =(bounds.Width - w)/ 2;
int y =(bounds.Height - h)/ 2;
gDrawImage(img,new Rectangle(x,y,w,h));
}

为了完美地测试你的表单,你还必须设置 ResizeRedraw = true 并启用 DoubleBuffered

  public Form1(){
InitializeComponent();
ResizeRedraw = true;
DoubleBuffered = true;
}


When you load an image into a PictureBox there is a zoom for the image placement, how do I achieve that same effect with a graphics object?

解决方案

I think you mean you want to draw some Image yourself in a Rectangle and using some Graphics object like as the PictureBox renders its Image in Zoom mode. Try the following code. I suppose you want to draw the Image on a Form, the drawing code should be added in a Paint event handler of your form:

//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));
}

To test it on your form perfectly, you should also have to set ResizeRedraw = true and enable DoubleBuffered:

public Form1(){
  InitializeComponent();
  ResizeRedraw = true;
  DoubleBuffered = true;
}

这篇关于使用图形对象的PictureBox缩放模式效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆