C# 如何使用PictureBoxizeMode.Zoom绘制图像
在我的应用程序中,我需要打印员工照片作为身份证。我使用了图片框控件和sizemode作为PictureBoxSizeMode.StretchImage。 打印时,照片会根据图片框的宽度和高度变宽。但是这张照片看起来不像是原作。 当我将sizemode设置为PictureBoxSizeMode时,它就完美了。放大设计器窗口。但在打印时,结果将与以前相同。没有效果C# 如何使用PictureBoxizeMode.Zoom绘制图像,c#,C#,在我的应用程序中,我需要打印员工照片作为身份证。我使用了图片框控件和sizemode作为PictureBoxSizeMode.StretchImage。 打印时,照片会根据图片框的宽度和高度变宽。但是这张照片看起来不像是原作。 当我将sizemode设置为PictureBoxSizeMode时,它就完美了。放大设计器窗口。但在打印时,结果将与以前相同。没有效果 PictureBox pict = (PictureBox)ctrl; pict.SizeMode = PictureBoxSizeMo
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事件时将执行上述代码我想在单击打印按钮之前,您可以尝试在
缩放
模式下捕获PictureBox
的位图,如下所示:
//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));
}
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);
抱歉,但我已经意识到此代码与我之前(不久前)在回答中发布的代码非常相似?@King-它违反了此网站的许可条款。你可能需要解释一下。要求是指向原始帖子的链接以及指向该帖子作者简介的链接。SizeMode完全不影响图像,只影响PictureBox用于绘制图像的Graphics.DrawImage()调用。您必须在自己的代码中复制这些内容。否则,使用Graphics.ScaleTransform()很容易
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);