0

在我的应用程序中,我创建了一个printdocument. 现在我想转换printdocumentimage.

我怎样才能做到这一点?

在我之前使用这样的编码。(如果有其他解决方案,请在图像中画出每一行?易于转换printdocument to image

  Dim printArea As RectangleF = New RectangleF(0, 0, 13.6 * mInch, 21.1 * mInch)
            'create a bmp image for specified rectangle
            Dim bm As New Bitmap(Convert.ToInt32(Math.Floor(printArea.Width)), Convert.ToInt32(Math.Floor(printArea.Height)))
            'bm.SetPixel(13.5 * mInch, 21 * mInch, Color.Blue)
            'create the graphics of bmp image 
            Dim g As Graphics = Graphics.FromImage(bm)

            If Rdr.Read Then

                g.DrawLine(Pens.Black, CInt(0.4 * mInch), CInt(0.4 * mInch), CInt(13.5 * mInch), CInt(0.4 * mInch))

                'Bottom border line
                g.DrawLine(Pens.Black, CInt(0.4 * mInch), CInt(21 * mInch), CInt(13.5 * mInch), CInt(21 * mInch))

                'Left border line
                g.DrawLine(Pens.Black, CInt(0.4 * mInch), CInt(0.4 * mInch), CInt(0.4 * mInch), CInt(21 * mInch))

                'Right border line
                g.DrawLine(Pens.Black, CInt(13.5 * mInch), CInt(0.4 * mInch), CInt(13.5 * mInch), CInt(21 * mInch))

                xTop = 0.4

                If File.Exists(xPath) = True Then
                    g.DrawImage(Image.FromFile(xPath), _
                             CInt(0.8 * mInch), CInt(0.7 * mInch), CInt(4.5 * mInch), CInt(1.5 * mInch))
                End If

                ' g.DrawString("Gloier", New Font(mTextFont.Name, mTextFont.Size + 4, FontStyle.Bold + FontStyle.Italic, GraphicsUnit.Point), Brushes.Black, 1.7 * mInch, xTop * mInch)

                xTop = 1.7

                drawRect = New RectangleF(1 * mInch, xTop * mInch, 12.0 * mInch, 0.6 * mInch)
                g.DrawString("RECEIPT", New Font(mTextFont.Name, mTextFont.Size + 3, FontStyle.Bold + FontStyle.Italic, GraphicsUnit.Point), Brushes.Black, drawRect, xformat)

                xTop = xTop + 1.8
                g.DrawString("No.", mTextFont, Brushes.Black, 8.5 * mInch, xTop * mInch)
                g.DrawString(":", mTextFont, Brushes.Black, 9.5 * mInch, xTop * mInch)
                g.DrawString(Rdr.Item("receiptno"), New Font(mTextFont.Name, mTextFont.Size + 2, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 10.0 * mInch, xTop * mInch)
                xTop = xTop + 0.8
                g.DrawString("Date", mTextFont, Brushes.Black, 8.5 * mInch, xTop * mInch)
                g.DrawString(":", mTextFont, Brushes.Black, 9.5 * mInch, xTop * mInch)
                g.DrawString(Rdr.Item("edate"), mTextFont, Brushes.Black, 10.0 * mInch, xTop * mInch)

                ''---Hr.Line
                xTop = xTop + 1
                g.DrawLine(Pens.Black, CInt(0.4 * mInch), CInt(xTop * mInch), CInt(13.5 * mInch), CInt(xTop * mInch))

                xTop = xTop + 0.3
                'xTop = 6.1
                g.DrawString("Account", New Font(mTextFont.Name, mTextFont.Size, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 0.7 * mInch, xTop * mInch)

                xTop = xTop + 0.7
                ''---Hr.Line
                g.DrawLine(Pens.Black, CInt(0.4 * mInch), CInt(xTop * mInch), CInt(13.5 * mInch), CInt(xTop * mInch))

                xTop = xTop + 0.3

                drawRect = New RectangleF(0.7 * mInch, xTop * mInch, 11.5 * mInch, 0.8 * mInch)
                g.DrawString(Rdr.Item("salutation") & " " & Rdr.Item("partyname"), New Font(mTextFont.Name, mTextFont.Size, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, drawRect)

                If (Rdr.Item("salutation") & " " & Rdr.Item("partyname")).Length > 80 Then
                    xTop = xTop + (0.5 * 2)
                Else
                    xTop = xTop + 0.5
                End If

                Dim xLength As Integer
                If (Rdr.Item("doorno") & "," & Rdr.Item("streetname")).ToString.Length > 80 Then xLength = 80 Else xLength = (Rdr.Item("doorno") & "," & Rdr.Item("streetname")).ToString.Length - 1

                g.DrawString((Rdr.Item("doorno") & "," & Rdr.Item("streetname")).ToString.Substring(0, xLength), mTextFont, Brushes.Black, 0.7 * mInch, xTop * mInch)

                xTop = xTop + 0.5
                If (Rdr.Item("area") & "," & Rdr.Item("city")).ToString.Length > 80 Then xLength = 80 Else xLength = (Rdr.Item("area") & "," & Rdr.Item("city")).ToString.Length - 1
                g.DrawString((Rdr.Item("area") & "," & Rdr.Item("city")).ToString.Substring(0, xLength), mTextFont, Brushes.Black, 0.7 * mInch, xTop * mInch)


                If Not Trim(Rdr.Item("remarks")) = String.Empty Then
                    xTop = xTop + 0.6
                    xformat.Alignment = StringAlignment.Near
                    drawRect = New RectangleF(0.7 * mInch, xTop * mInch, 11.5 * mInch, 2.0 * mInch)
                    g.DrawString(Rdr.Item("remarks"), mTextFont, Brushes.Black, drawRect)
                End If

                xTop = 10.5
                g.DrawString("Mode", mTextFont, Brushes.Black, 0.7 * mInch, xTop * mInch)
                g.DrawString(":", mTextFont, Brushes.Black, 4.5 * mInch, xTop * mInch)
                g.DrawString(Rdr.Item("transtype"), New Font(mTextFont.Name, mTextFont.Size, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 4.8 * mInch, xTop * mInch)

                If Not Rdr.Item("transtype").ToString.ToUpper.Trim = "CASH" Then
                    xTop = xTop + 0.5
                    g.DrawString("Transaction Ref.No.", mTextFont, Brushes.Black, 0.7 * mInch, xTop * mInch)
                    g.DrawString(":", mTextFont, Brushes.Black, 4.5 * mInch, xTop * mInch)
                    g.DrawString(Rdr.Item("transrefno"), mTextFont, Brushes.Black, 4.8 * mInch, xTop * mInch)

                    xTop = xTop + 0.5
                    g.DrawString("Transaction Ref.Date", mTextFont, Brushes.Black, 0.7 * mInch, xTop * mInch)
                    g.DrawString(":", mTextFont, Brushes.Black, 4.5 * mInch, xTop * mInch)
                    g.DrawString(Rdr.Item("transrefdate"), mTextFont, Brushes.Black, 4.8 * mInch, xTop * mInch)

                    If Not Rdr.Item("bankname").ToString.Trim = String.Empty Then
                        xTop = xTop + 0.5
                        g.DrawString("Bank Name", mTextFont, Brushes.Black, 0.7 * mInch, xTop * mInch)
                        g.DrawString(":", mTextFont, Brushes.Black, 4.5 * mInch, xTop * mInch)
                        g.DrawString(Rdr.Item("bankname"), mTextFont, Brushes.Black, 4.8 * mInch, xTop * mInch)
                    End If

                End If

                xTop = 12.5
                ''---Hr.Line
                g.DrawLine(Pens.Black, CInt(0.4 * mInch), CInt(xTop * mInch), CInt(13.5 * mInch), CInt(xTop * mInch))

                xTop = xTop + 0.2

                g.DrawString("AMOUNT : " & ShareVarProfile2.Currency1, New Font(mTextFont.Name, mTextFont.Size, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, 8.5 * mInch, xTop * mInch)

                drawRect = New RectangleF(11.3 * mInch, (xTop - 0.05) * mInch, 3 * mInch, 0.4 * mInch)
                xformat.Alignment = StringAlignment.Far
                g.DrawString(Format(Rdr.Item("rcvdamount"), ShareVarProfile2.DecimalFormatAmt), New Font(mTextFont.Name, mTextFont.Size + 1, FontStyle.Bold, GraphicsUnit.Point), Brushes.Black, drawRect, xformat)
                '       
                xTop = xTop + 0.7
                ''---Hr.Line

                g.DrawLine(Pens.Black, CInt(0.4 * mInch), CInt(xTop * mInch), CInt(13.5 * mInch), CInt(xTop * mInch))

                xTop = xTop + 0.75

                xformat.Alignment = StringAlignment.Near
                xformat.FormatFlags = StringFormatFlags.NoClip
                drawRect = New RectangleF(0.7 * mInch, xTop * mInch, 12 * mInch, 0.8 * mInch)
                g.DrawString("Received " & OwnFunctions.RsInWords(Rdr.Item("rcvdamount")), New Font(mTextFont.Name, mTextFont.Size, FontStyle.Italic), Brushes.Black, drawRect, xformat)


                If Rdr.Item("transtype").ToString.Trim.ToUpper = "CHEQUE" Then
                    xTop = xTop + 1.5
                    g.DrawString("Note : Cheque Subject to Jurisdiction in Tirunelveli only.", New Font(mTextFont.Name, mTextFont.Size - 1, GraphicsUnit.Point), Brushes.Black, 0.7 * mInch, xTop * mInch)
                End If

                g.DrawString("For " & ShareVarProfile2.CompName, mTextFont, Brushes.Black, 10.5 * mInch, 17.8 * mInch)

                g.DrawString("Party Signature With Seal", mTextFont, Brushes.Black, 0.7 * mInch, 17.8 * mInch)

                g.DrawString(mRcptSign, mTextFont, Brushes.Black, 0.7 * mInch, 17.3 * mInch)

                xformat.Alignment = StringAlignment.Center
                drawRect = New RectangleF(1 * mInch, 18.2 * mInch, 11.8 * mInch, 0.8 * mInch)
                g.DrawString(mRcptFooter, New Font(mTextFont.Name, mTextFont.Size - 2, GraphicsUnit.Point), Brushes.Black, drawRect, xformat)

                xTop = 18.8

                Dim xAddress As String = String.Empty

                If ShareVarProfile2.Address1 <> String.Empty And ShareVarProfile2.Address2 <> String.Empty Then
                    xAddress = ShareVarProfile2.Address1 & ", " & ShareVarProfile2.Address2 & " | "
                ElseIf ShareVarProfile2.Address1 <> String.Empty Then
                    xAddress = ShareVarProfile2.Address1 & " | "
                Else
                    xAddress = ShareVarProfile2.Address2 & " | "
                End If

                If ShareVarProfile2.Address3 <> String.Empty Then
                    xAddress += ShareVarProfile2.Address3 & " | "
                End If

                If ShareVarProfile2.Area <> String.Empty Then
                    xAddress += ShareVarProfile2.Area & " | "
                End If

                If ShareVarProfile2.District <> String.Empty Then
                    xAddress += ShareVarProfile2.District & " | "
                End If

                If ShareVarProfile2.State <> String.Empty Then
                    xAddress += ShareVarProfile2.State & " | "
                End If

                If ShareVarProfile2.PinCode <> String.Empty Then
                    xAddress += ShareVarProfile2.PinCode
                End If

                If xAddress <> String.Empty Then If xAddress.Substring(xAddress.Length - 1, 1) = "|" Then xAddress = xAddress.Remove(xAddress.Length - 1, 1)

                xformat.Alignment = StringAlignment.Far
                drawRect = New RectangleF(1 * mInch, xTop * mInch, 11.8 * mInch, 0.4 * mInch)
                If xAddress <> String.Empty Then
                    g.DrawString(xAddress, New Font(mTextFont.Name, mTextFont.Size - 3, GraphicsUnit.Point), Brushes.Black, drawRect, xformat)
                    xTop += 0.4
                End If

                Dim xEmailWeb As String = String.Empty

                If ShareVarProfile2.EMail <> String.Empty And ShareVarProfile2.WebSite <> String.Empty Then
                    xEmailWeb = ShareVarProfile2.EMail & " | " & ShareVarProfile2.WebSite
                ElseIf ShareVarProfile2.EMail <> String.Empty Then
                    xEmailWeb = ShareVarProfile2.EMail
                Else
                    xEmailWeb = ShareVarProfile2.WebSite
                End If

                drawRect = New RectangleF(1 * mInch, xTop * mInch, 11.8 * mInch, 0.4 * mInch)
                If xEmailWeb <> String.Empty Then
                    g.DrawString(xEmailWeb, New Font(mTextFont.Name, mTextFont.Size - 3, GraphicsUnit.Point), Brushes.Black, drawRect, xformat)
                    xTop += 0.4
                End If

                Dim xTinCst As String = String.Empty

                If ShareVarProfile2.StaturyNo1 <> String.Empty And ShareVarProfile2.StaturyNo2 <> String.Empty Then
                    xTinCst = "TIN : " & ShareVarProfile2.EMail & " | CST No.: " & ShareVarProfile2.StaturyNo2
                ElseIf ShareVarProfile2.StaturyNo1 <> String.Empty Then
                    xTinCst = "TIN : " & ShareVarProfile2.StaturyNo1
                Else
                    xTinCst = "CST No.: " & ShareVarProfile2.StaturyNo2
                End If

                drawRect = New RectangleF(1 * mInch, xTop * mInch, 11.8 * mInch, 0.4 * mInch)
                If xTinCst <> String.Empty Then
                    g.DrawString(xTinCst, New Font(mTextFont.Name, mTextFont.Size - 3, GraphicsUnit.Point), Brushes.Black, drawRect, xformat)
                    xTop += 0.4
                End If

                xTop = 20
                ''---Hr.Line
                g.DrawLine(Pens.Black, CInt(0.4 * mInch), CInt(xTop * mInch), CInt(13.5 * mInch), CInt(xTop * mInch))

                xTop += 0.3
                g.DrawString("UNBEATABLE QUALITY", New Font(mTextFont.Name, mTextFont.Size + 2, GraphicsUnit.Point), Brushes.Black, 1 * mInch, xTop * mInch)

                drawRect = New RectangleF(6 * mInch, xTop * mInch, 7.5 * mInch, 0.8 * mInch)
                g.DrawString(ShareVarProfile2.PhoneNo, New Font(mTextFont.Name, mTextFont.Size + 2, GraphicsUnit.Point), Brushes.Black, drawRect, xformat)


            End If

            Rdr.Close()

            bm.Save(Application.StartupPath & "\Documents\Receipt" & Val(xSplitReciptTmNo(0)) & ".bmp")

我正在使用Vb.Net 2008

4

1 回答 1

1

有两种方法可以达到它:

  1. 直接将 printdoc 保存到图像.. http://www.dreamincode.net/forums/topic/70901-save-printdocument-to-image/

  2. 安装Image Printer Driver并将其设置为 printdoc 打印机设备

于 2013-09-23T06:02:09.483 回答