2010-04-14 3 views
1

저는 Graphics.ScaleTransform을 사용하여 텍스트 줄을 늘려 페이지 너비에 맞게 조정 한 다음 해당 페이지를 인쇄합니다. 그러나이 작업은 인쇄 작업을 비트 맵으로 변환합니다. 많은 페이지를 인쇄 할 경우 인쇄 작업의 크기가 음란물 비율로 증가하고 인쇄 속도가 크게 떨어집니다..NET Graphics.ScaleTransform은 인쇄 작업을 비트 맵으로 변환합니다. 텍스트 크기를 조정하는 다른 방법은 없습니까?

이렇게 크기를 조정하지 않으면 인쇄 작업이 프린터로 텍스트 인쇄 명령을 보내는 것과 같이 매우 작습니다.

제 질문은 Graphics.ScaleTransform을 사용하여 텍스트의 너비를 늘리는 것 외에 다른 방법이 있습니까? 이를 증명하기

샘플 코드 (인쇄 작업에 확장의 효과를 보여 Print.Test(True)Print.Test(False) 호출 할 것이다) 다음과 같습니다 :

Imports System.Drawing 
Imports System.Drawing.Printing 
Imports System.Drawing.Imaging 

Public Class Print 

    Dim FixedFont As Font 
    Dim Area As RectangleF 
    Dim CharHeight As Double 
    Dim CharWidth As Double 
    Dim Scale As Boolean 

    Const CharsAcross = 80 
    Const CharsDown = 66 
    Const TestString = "!""#$%&'()*+,-./:;<=>[email protected][\]^_`abcdefghijklmnopqrstuvwxyz{|}~" 

    Private Sub PagePrinter(ByVal sender As Object, ByVal e As PrintPageEventArgs) 

     Dim G As Graphics = e.Graphics 
     If Scale Then 
      Dim ws = Area.Width/G.MeasureString(Space(CharsAcross).Replace(" ", "X"), FixedFont).Width 
      G.ScaleTransform(ws, 1) 
     End If 

     For CurrentLine = 1 To CharsDown 
      G.DrawString(Mid(TestString & TestString & TestString, CurrentLine, CharsAcross), FixedFont, Brushes.Black, 0, Convert.ToSingle(CharHeight * (CurrentLine - 1))) 
     Next 

     e.HasMorePages = False 

    End Sub 

    Public Shared Sub Test(ByVal Scale As Boolean) 

     Dim OutputDocument As New PrintDocument 
     With OutputDocument 
      Dim DP As New Print 
      .PrintController = New StandardPrintController 
      .DefaultPageSettings.Landscape = False 
      DP.Area = .DefaultPageSettings.PrintableArea 
      DP.CharHeight = DP.Area.Height/CharsDown 
      DP.CharWidth = DP.Area.Width/CharsAcross 
      DP.Scale = Scale 
      DP.FixedFont = New Font("Courier New", DP.CharHeight/100, FontStyle.Regular, GraphicsUnit.Inch) 
      .DocumentName = "Test print (with" & IIf(Scale, "", "out") & " scaling)" 
      AddHandler .PrintPage, AddressOf DP.PagePrinter 
      .Print() 
     End With 
    End Sub 
End Class 

업데이트 : GDI와 상호 운용성 사용 대신 호출합니다. 다음은 관련 코드입니다. GDI 클래스는 위키에서 복사 한 정의로 가득 차 있습니다. 관련 함수 및 상수는 http://pinvoke.net/입니다.

' convert from Graphics units (100 dpi) to device units 
    Dim GDIMappedCharHeight As Double = CharHeight * G.DpiY/100 
    Dim GDIMappedCharWidth As Double = CharWidth * G.DpiX/100 

    Dim FixedFontGDI As IntPtr = GDI.CreateFont(GDIMappedCharHeight, GDIMappedCharWidth, 0, 0, 0, 0, 0, 0, GDI.DEFAULT_CHARSET, GDI.OUT_DEFAULT_PRECIS, GDI.CLIP_DEFAULT_PRECIS, GDI.DEFAULT_QUALITY, GDI.FIXED_PITCH, "Courier New") 
    Dim CharRect As New GDI.STRUCT_RECT 

    Dim hdc As IntPtr = G.GetHdc() 
    GDI.SelectObject(hdc, FixedFontGDI) 

    ' I used SetBkMode transparent as my text needed to overlay a background 
    GDI.SetBkMode(hdc, GDI.TRANSPARENT) 

    ' draw it character by character to get precise grid 
    For CurrentLine = 1 To CharsDown 
     For CurrentColumn = 1 To CharsAcross 
      With CharRect 
       .left = GDIMappedCharWidth * (CurrentColumn - 1) 
       .right = GDIMappedCharWidth * CurrentColumn 
       .top = GDIMappedCharHeight * (CurrentLine - 1) 
       .bottom = GDIMappedCharHeight * CurrentLine 
      End With 
      ' 2341 == DT_NOPREFIX|DT_NOCLIP|DT_VCENTER|DT_CENTER|DT_SINGLELINE 
      GDI.DrawText(hdc, Mid(TestString & TestString & TestString, CurrentLine+CurrentColumn, 1), 1, CharRect, 2341) 
     Next 
    Next 

    GDI.DeleteObject(FixedFontGDI) 

    G.ReleaseHdc(hdc) 

답변

1

예, Graphics 클래스는 크기 조정 텍스트를 지원합니다. 그러나 먼저 텍스트를 비트 맵으로 렌더링하고 비트 맵의 ​​크기를 조정 한 다음 크기 조정 된 비트 맵을 프린터 드라이버로 전달해야합니다. 이러한 모든 비트 맵은 큰 스풀러 파일을 만듭니다.

텍스트를 직접 입력해야합니다. 이 프레임 워크에는 아무런 지원이 없습니다. 이를 수행하는 한 가지 방법은 풍부한 편집 컨트롤을 납치하여 정당화 및 인쇄를 처리하는 것입니다. 버전 5, msftedit.dll, 전체 칭의를 지원합니다. 필요한 코드를 찾는 가장 좋은 방법은 Windows의 워드 패드와 유사한 RTB가있는 텍스트 편집기를 구현하는 많은 프로젝트 중 하나를 찾는 것입니다.

+0

귀하의 조언에 감사드립니다! 풍부한 편집 컨트롤을 사용해 보았지만 압도적 인 것처럼 보였으므로 오래된 GDI를 사용하여 interop를 사용하여 그래픽 hDC를 호출했습니다. 다행히도 CreateFont는 너비와 높이를 설정하는 것을 지원하며이를 전달합니다. 비트 맵 렌더링이없는 프린터. –

0

여기에서 추측하고 있지만 크기를 늘릴 비율의 백분율로 글꼴 크기를 늘려야합니다.

+0

줄의 높이에 비례하여 [새 글꼴 ("Courier New", DP.CharHeight/100, FontStyle.Regular, GraphicsUnit.Inch) 코드에 표시된 글꼴의 높이를 설정했으나 그것의 폭을 독립적으로 설정하십시오. 불행히도 크기를 조절하면 크기와 높이가 같은 비율로 늘어납니다. –