2013-11-26 3 views
0

내 애플리케이션에서 ID 배지로 직원 사진을 인쇄해야합니다. PictureBoxSizeMode.StretchImage로 그림 상자 컨트롤과 sizemode를 사용했습니다. 이 사진을 인쇄하면 사진 상자 너비 및 높이에 따라 사진이 더 넓어집니다. 그러나 사진은 원본 사진처럼 보이지 않습니다. 디자이너 윈도우에서 sizemode를 PictureBoxSizeMode.Zoom으로 설정할 때 완벽합니다. 그러나 인쇄하는 동안 결과는 이전과 동일합니다. 아무 효과가 없습니다.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); 

위의 코드는 당신이 또한 ResizeRedraw = TRUE를 설정해야하고 활성화해야의 PrintPage 이벤트가

+0

SizeMode는 이미지에 전혀 영향을주지 않으며 PictureBox에서 이미지를 그릴 때 사용하는 Graphics.DrawImage() 호출에만 영향을줍니다. 어떤 코드를 자신의 코드로 재현해야합니다. 그렇지 않으면 Graphics.ScaleTransform()을 사용하여 간단하게 처리 할 수 ​​있습니다. –

답변

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

완벽 양식에서 테스트하기 위해 트리거 될 때 실행됩니다 DoubleBuffered :

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

죄송합니다.하지만이 코드는이 답변에서 이전에 게시 한 코드와 매우 유사하게 보입니다. http://stackoverflow.com/questions/20101882/picturebox -zoom-mode-effect-with-graphics-object/20107316 # 20107316? –

+2

@ 킹 -이 웹 사이트의 라이센스 조항을 위반합니다. 아마도 그것을 설명 할 필요가있을 것입니다. 요구 사항은 원래 게시물의 링크뿐만 아니라 해당 게시물 작성자의 프로필에 대한 링크입니다. –

+1

[이것은 표절 직선입니다] (http://stackoverflow.com/a/20107316). –

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); 
관련 문제