2012-06-03 4 views
3

사용자가 선택한 항목의 문서를 인보이스 형태로 인쇄 할 수있는 응용 프로그램이 있습니다. 그러나 모든 것이 잘 작동합니다 PrintDocument의 PrintPage 이벤트에서 문서 또는 그래픽을 캡처하고 비트 맵으로 변환하여 나중에 사용 /보기 위해 .bmp으로 저장할 수 있습니다. (참고 :이 문서에 여러 페이지가 있습니다) 내가이 같은 설정이 있습니다인쇄 페이지 그래픽을 비트 맵으로 변환 C#

PrintPage 이벤트에 다음
PrintDocument doc = new PrintDocument(); 
doc.PrintPage += new PrintPageEventHandler(doc_PrintPage); 
doc.Print(); 

: 난 그냥 때문에 모든 ev.Graphics 코드를 잘라

private void doc_PrintPage(object sender, PrintPageEventArgs ev) 
{ 
    // Use ev.Graphics to create the document 
    // I create the document here 

    // After I have drawn all the graphics I want to get it and turn it into a bitmap and save it. 
} 

라인이 많이 있습니다. PrintDocument에 그래픽을 그리는 코드를 변경하지 않고 그래픽을 비트 맵으로 변환 할 수 있습니까? 또는 문서를 복사하여 비트 맵으로 변환하는 것과 유사한 작업을 수행할까요?

답변

5

실제로 비트 맵에 페이지를 그린 다음 ev.Graphics를 사용하여 페이지에 비트 맵을 그려야합니다.

private void doc_PrintPage(object sender, PrintPageEventArgs ev) 
{ 
    var bitmap = new Bitmap((int)graphics.ClipBounds.Width, 
          (int)graphics.ClipBounds.Height); 

    using (var g = Graphics.FromImage(bitmap)) 
    { 
     // Draw all the graphics using into g (into the bitmap) 
     g.DrawLine(Pens.Black, 0, 0, 100, 100); 
    } 

    // And maybe some control drawing if you want...? 
    this.label1.DrawToBitmap(bitmap, this.label1.Bounds); 

    ev.Graphics.DrawImage(bitmap, 0, 0); 
} 
+0

감사! 나는 이것을 곧 시도 할 것이고 그것이 효과가 있다면 나는 다시보고 할 것이다. – 3aw5TZetdf

+0

감사합니다. – 3aw5TZetdf

0

실제로 6 월 3 일 12시 7 분 33 초에 대한 대답은 정확하고 나를 도왔던 출발점이었습니다. 그러나, 나는 그것을 그대로 작동시킬 수 없었기 때문에, 그것을 나의 응용에서 작동하도록 약간의 수정을했다. 수정 작업은 PrintPgeEventArgs.Graphics 장치 컨텍스트에서 프린터의 페이지 크기를 가져오고 PrintPage Graphics를 새 Bitmap (...) 구성의 세 번째 매개 변수로 포함시키는 것입니다.

private void doc_PrintPage(object sender, PrintPageEventArgs ppea) 
{ 
    // Retrieve the physical bitmap boundaries from the PrintPage Graphics Device Context 
    IntPtr hdc = ppea.Graphics.GetHdc(); 
    Int32 PhysicalWidth = GetDeviceCaps(hdc, (Int32)PHYSICALWIDTH); 
    Int32 PhysicalHeight = GetDeviceCaps(hdc, (Int32)PHYSICALHEIGHT); 
    ppea.Graphics.ReleaseHdc(hdc); 

    // Create a bitmap with PrintPage Graphic's size and resolution 
    Bitmap myBitmap = new Bitmap(PhysicalWidth, PhysicalHeight, ppea.Graphics); 
    // Get the new work Graphics to use to draw the bitmap 
    Graphics myGraphics = Graphics.FromImage(myBitmap); 

    // Draw everything on myGraphics to build the bitmap 

    // Transfer the bitmap to the PrintPage Graphics 
    ppea.Graphics.DrawImage(myBitmap, 0, 0); 

    // Cleanup 
    myBitmap.Dispose(); 
} 

//////// 
// Win32 API GetDeviceCaps() function needed to get the DC Physical Width and Height 

const int PHYSICALWIDTH = 110; // Physical Width in device units   
const int PHYSICALHEIGHT = 111; // Physical Height in device units   

// This function returns the device capability value specified 
// by the requested index value. 
[DllImport("GDI32.DLL", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 nIndex); 

원본 답변을 제공 한 Yorye Nathan에게 다시 한 번 감사드립니다. // AJ

관련 문제