在我的应用程序中,我需要将员工照片打印为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事件被触发时执行
答案 0 :(得分:1)
我认为在点击打印按钮之前,您可以尝试在PictureBox
模式下捕获Zoom
的位图,如下所示:
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);
答案 1 :(得分:-1)
//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;
}