I am using Zen Barcode Rendering Framework to create bar codes in VB.NET windows form application.
From the Winform application I want to print directly to the barcode printer via graphic without using the language of the printer. Is there something wrong with the code please guide me.
Thanks
Private Sub BtnScreen_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnScreen.Click
' if there was a previous image in the picture box, dispose of it now
If PicCode.Image IsNot Nothing Then
PicCode.Image.Dispose()
End If
' create a 24 bit image that is the size of your picture box
Dim img = New Bitmap(PicCode.Width, PicCode.Height, PixelFormat.Format24bppRgb)
' wrap it in a graphics object
Using g = Graphics.FromImage(img)
' send that graphics object to the rendering code
RenderBarcodeInfoToGraphics(g, TxtCode.Text, TxtInfo.Text, New Rectangle(0, 0, PicCode.Width, PicCode.Height))
End Using
' set the new image in the picture box
PicCode.Image = img
End Sub
Private Sub BtnPrinter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnPrinter.Click
' create a document that will call the same rendering code but
' this time pass the graphics object the system created for that device
Dim doc = New PrintDocument()
AddHandler doc.PrintPage, Sub(s, printArgs)
' send that graphics object to the rendering code using the size
' of the media defined in the print arguments
RenderBarcodeInfoToGraphics(printArgs.Graphics, TxtCode.Text, TxtInfo.Text, printArgs.PageBounds)
End Sub
' save yourself some paper and render to a print-preview first
Using printPrvDlg = New PrintPreviewDialog With {.Document = doc}
printPrvDlg.ShowDialog()
End Using
' finally show the print dialog so the user can select a printer
' and a paper size (along with other miscellaneous settings)
Using pd = New PrintDialog With {.Document = doc}
If pd.ShowDialog() = DialogResult.OK Then
doc.Print()
End If
End Using
End Sub
''' <summary>
''' This method will draw the contents of the barcode parameters to any
''' graphics object you pass in.
''' </summary>
''' <param name="g">The graphics object to render to</param>
''' <param name="code">The barcode value</param>
''' <param name="info">The information to place under the bar code</param>
''' <param name="rect">The rectangle in which the design is bound to</param>
Private Shared Sub RenderBarcodeInfoToGraphics(ByVal g As Graphics, ByVal code As String, ByVal info As String, ByVal rect As Rectangle)
' Constants to make numbers a little less magical
Const barcodeHeight As Integer = 50
Const marginTop As Integer = 20
Const codeFontFamilyName As String = "Courier New"
Const codeFontEmSize As Integer = 10
Const marginCodeFromCode As Integer = 10
Const infoFontFamilyName As String = "Arial"
Const infoFontEmSize As Integer = 12
Const marginInfoFromCode As Integer = 10
' white background
g.Clear(Color.White)
' We want to make sure that when it draws, the renderer doesn't compensate
' for images scaling larger by blurring the image. This will leave your
' bars crisp and clean no matter how high the DPI is
g.InterpolationMode = InterpolationMode.NearestNeighbor
' generate barcode
Using img = BarcodeDrawFactory.Code128WithChecksum.Draw(code, barcodeHeight)
' daw the barcode image
g.DrawImage(img, New Point(rect.X + (rect.Width \ 2 - img.Width \ 2), rect.Y + marginTop))
End Using
' now draw the code under the bar code
Using br = New SolidBrush(Color.Black)
' calculate starting position of text from the top
Dim yPos = rect.Y + marginTop + barcodeHeight + marginCodeFromCode
' align text to top center of area
Dim sf = New StringFormat With {
.Alignment = StringAlignment.Center,
.LineAlignment = StringAlignment.Near
}
' draw the code, saving the height of the code text
Dim codeTextHeight = 0
Using font As New Font(codeFontFamilyName, codeFontEmSize, FontStyle.Regular)
codeTextHeight = CInt(Math.Truncate(Math.Round(g.MeasureString(code, font).Height)))
g.DrawString(code, font, br, New Rectangle(rect.X, yPos, rect.Width, 0), sf)
End Using
' draw the info below the code
Using font As New Font(infoFontFamilyName, infoFontEmSize, FontStyle.Regular)
g.DrawString(info, font, br, New Rectangle(rect.X, yPos + codeTextHeight + marginInfoFromCode, rect.Width, 0), sf)
End Using
End Using
End Sub
RESULT IN FORM
RESULT IN PRINT PREVIEW
RESULT OUTPUT IN LABEL
Label Dimensions



