Printing on a thermal printer with an XML response

beau kang

New member
Joined
Feb 23, 2009
Messages
1
Programming Experience
1-3
I'm getting an image base64 encoded and I need to print it through a thermal printer for the shipping labels. I can get the printer to print, but the information doesn't seem to be feeding into the **** thing, creating just a blank page. I had someone tell me before to try just sending the bytes into the machine, but unfortunately, I'm still kinda novice on this thing and am unclear as on how to do this. Here is the code that I've been using thus far:
VB.NET:
Dim imageFormat As XmlElement
            Dim graphicImage As XmlElement
            graphicImage = response.DocumentElement.SelectSingleNode("ShipmentResults/PackageResults/LabelImage/GraphicImage")
            imageFormat = response.DocumentElement.SelectSingleNode("ShipmentResults/PackageResults/LabelImage/LabelImageFormat/Code")

            'get image data
            If Not (graphicImage Is Nothing) Then
                Dim base64String As String
                Dim bytes() As Byte
                Dim ms As MemoryStream

                base64String = graphicImage.InnerText
                bytes = Convert.FromBase64String(base64String)

                ms = New MemoryStream(bytes, 0, bytes.Length)
                ms.Write(bytes, 0, bytes.Length)
                'EPL image conversion breaks here 2/12/09 -Beau
                If (imageFormat.InnerText = "GIF") Then
                    Me.image = Drawing.Image.FromStream(ms, True)
                    'Else
                    'Dim SendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(bytes.ToString()) 'EPL needs ASCII
                    'ms = New MemoryStream(SendBytes, 0, SendBytes.Length)
                    'ms.Write(SendBytes, 0, SendBytes.Length)
                End If

                ms.Flush()
                ms.Close()
            End If

            'print the image
            Dim pd As New PrintDocument

            'Testing to see if we can control which printer is used 1/29/09 -Beau
            Dim usePrinter As String = ""
            Dim PRINTER_FILE_NAME As String = "C:\YandyShippingSolution\App\printer.txt" 'Use a local file to get right printer name
            If System.IO.File.Exists(PRINTER_FILE_NAME) = True Then
                Dim objReader As New System.IO.StreamReader(PRINTER_FILE_NAME)
                Do While objReader.Peek <> -1
                    usePrinter = objReader.ReadLine()
                Loop
                pd.PrinterSettings.PrinterName = usePrinter.ToString()
            Else
                pd.PrinterSettings.DefaultPageSettings.Landscape = True    'Landscape for regular printer
            End If
            'End printer control testing

            If (imageFormat.InnerText = "GIF") Then 'Calls function for regular image
                AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
                pd.Print()
            Else
                MsgBox("EPL printing")
                pd.Print()
            End If

And this is the handler being used for when the image is just a GIF image:
VB.NET:
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim rect As RectangleF
        'Dim height As Single
        'Dim width As Single
        If Not Me.printedLabel Then
            'CHANGED BELOW, REPLACED print.PrintWidth, print.PrintHeight TO 600, 400
            'rect = New RectangleF(0, 0, print.PrintWidth, print.PrintHeight)
            rect = New RectangleF(0, 0, 600, 400)
            ev.Graphics.DrawImage(image, rect)
            Me.printedLabel = True
            If Not (customsImage Is Nothing) And Not (ev.HasMorePages) Then
                ev.HasMorePages = True
            End If
        Else
            If Not (customsImage Is Nothing) Then
                'CHANGED BELOW, REPLACED print.PrintWidth, print.PrintHeight TO 600, 400
                'rect = New RectangleF(0, 0, print.PrintWidth, print.PrintHeight)
                rect = New RectangleF(0, 0, 600, 400)
                ev.Graphics.DrawImage(customsImage, rect)
            End If
        End If
    End Sub

If anyone has an idea where I can at least begin so that I can get this thing to work, I'd appreciate it.
 
Back
Top