Visual C# 언어 개념

코드: 인쇄 미리 보기로 폼 보기(Visual C#)

이 예제에서는 현재 폼의 복사본을 인쇄 미리 보기로 보는 방법을 보여 줍니다.

예제
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
   Graphics mygraphics = this.CreateGraphics();
   Size s = this.Size;
   memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
   Graphics memoryGraphics = Graphics.FromImage(memoryImage);
   IntPtr dc1 = mygraphics.GetHdc();
   IntPtr dc2 = memoryGraphics.GetHdc();
   BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
   mygraphics.ReleaseHdc(dc1);
   memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
   CaptureScreen();
   printPreviewDialog1.Show();
}
코드 컴파일

이 예제를 컴파일하려면 다음이 필요합니다.

  • PrintPage 이벤트 처리기가 있는 printDocument1이라는 PrintDocument 구성 요소
  • Document 속성이 printDocument1로 설정된 printPreviewDialog1이라는 PrintPreviewDialog 구성 요소
  • Click 이벤트 처리기가 있는 printButton이라는 Button 컨트롤

예제 코드가 기존의 이벤트 처리기를 대체합니다. printButton을 클릭하면 폼의 인쇄 미리 보기가 표시됩니다.

강력한 프로그래밍

다음의 경우에는 예외가 발생합니다.

  • 프린터에 액세스할 수 있는 권한이 없는 경우
  • 관리되지 않는 코드를 사용할 수 있는 권한이 없는 경우
  • 프린터가 설치되어 있지 않은 경우
  • 인쇄 미리 보기 대화 상자가 이미 사라진 경우 (인쇄 미리 보기 대화 상자를 닫은 경우)
보안

이 예제를 실행하려면 관리되지 않는 코드를 실행할 수 있는 권한과 프린터에 액세스할 수 있는 권한이 있어야 합니다.

참고 항목

그래픽 프로그래밍 예제 항목 | PrintDocument 구성 요소를 사용하여 인쇄 | GDI+를 사용하여 이미지 렌더링 | Windows Forms에서 그래픽 인쇄 | PrintDocument 클래스

+ Recent posts