如何使用PictureBoxSizeMode.Zoom绘制图像

how to draw an image with PictureBoxSizeMode.Zoom
2021-05-11
  •  译文(汉语)
  •  原文(英语)

在我的应用程序中,我需要将员工照片打印为ID徽章.我已使用图片框控件和sizemode作为PictureBoxSizeMode.StretchImage.打印此图像时,照片会根据图片框的宽度和高度变宽.但是这张照片看起来并不像原始照片.当我在设计器窗口中将sizemode设置为PictureBoxSizeMode.Zoom时,它是完美的.但是在打印时,结果将与以前相同.没有效果.

PictureBox pict = (PictureBox)ctrl;
pict.SizeMode = PictureBoxSizeMode.Zoom;
RectangleF rect = new RectangleF(pict.Location.X, pict.Location.Y, pict.Width, pict.Height);
e.Graphics.DrawImage(pict.Image, rect);

触发PrintPage事件时,将执行以上代码

速聊1:
SizeMode完全影响图像,只有PictureBox用于绘制图像的Graphics.DrawImage()调用.您必须将其复制到自己的代码中.否则可以通过使用Graphics.ScaleTransform()轻松完成
解决过程1

我想点击打印按钮之前,可以尝试捕捉你的位图PictureBoxZoom这样的模式:

PictureBox pict = (PictureBox)ctrl;
pict.SizeMode = PictureBoxSizeMode.Zoom;
var bm = new Bitmap(pict.ClientSize.Width, pict.ClientSize.Height);
pict.DrawToBitmap(bm, pict.ClientRectangle);
e.Graphics.DrawImage(bm, pict.Bounds);
解决过程2
//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并启用DoubleBuffered:

public Form1(){
  InitializeComponent();
  ResizeRedraw = true;
  DoubleBuffered = true;
}
速聊1:
抱歉,但我已经意识到该代码与我之前(不久)在此答案中发布的代码非常相似stackoverflow.com/questions/20101882/…?
速聊2:
-它违反了本网站的许可条款.您可能需要解释一下.要求是原始帖子的链接以及该帖子作者个人资料的链接.

In my application I need to print employee photo as ID Badge .I have used picture box control and sizemode as PictureBoxSizeMode.StretchImage. When printing this, the photo gets wider according to the Picture box Width and Height. But the photo doesn’t look like as an original one. Its perfect when I set sizemode as PictureBoxSizeMode.Zoom in designer window. But while printing, the result will be same as before. There is no effect.

PictureBox pict = (PictureBox)ctrl;
pict.SizeMode = PictureBoxSizeMode.Zoom;
RectangleF rect = new RectangleF(pict.Location.X, pict.Location.Y, pict.Width, pict.Height);
e.Graphics.DrawImage(pict.Image, rect);

The above code will execute when the PrintPage event is Triggered

Talk1:
SizeMode does not affect the image at all, only the Graphics.DrawImage() call that PictureBox uses to paint the image. Which you will have to reproduce in your own code. Otherwise simple to do by using Graphics.ScaleTransform()
Solutions1

I think before clicking on the print button, you can try capturing the bitmap of your PictureBox in Zoom mode like this:

PictureBox pict = (PictureBox)ctrl;
pict.SizeMode = PictureBoxSizeMode.Zoom;
var bm = new Bitmap(pict.ClientSize.Width, pict.ClientSize.Height);
pict.DrawToBitmap(bm, pict.ClientRectangle);
e.Graphics.DrawImage(bm, pict.Bounds);
Solutions2
//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;
}
Talk1:
Sorry but I've realized that this code looks very same to the code I posted before (not long) in this answer stackoverflow.com/questions/20101882/… ?
Talk2:
- it violates the license terms of this web site. You probably need to explain that. Requirements are a link to the original post as well as a link to the profile of the author of that post.
转载于:https://stackoverflow.com/questions/20217794/how-to-draw-an-image-with-pictureboxsizemode-zoom

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入