问题
I just want that the barcode will print the value of txt_betamount.Text
, for example, when i input 100
it will create a barcode and it will print it, In my case what ever i inputted in txt_betamount.Text
the barcode automatic like static print the 123456789012
i don't know where it comes from.
I have this class that generate a String and Photobox.Image in my Activity and I have this code in my form that calls this Print Class.
Imports IDAutomation.Windows.Forms.LinearBarCode
Public Class Print
Private Shared Lines As New Queue(Of String)
Private Shared _myfont As Font
Private Shared prn As Printing.PrintDocument
Public Shared Photo As Image
Public Shared pbImage2 As New PictureBox
Shared Sub New()
_myfont = New Font("Courier New",
11, FontStyle.Regular, GraphicsUnit.Point)
prn = New Printing.PrintDocument
AddHandler prn.PrintPage, AddressOf PrintPageHandler
End Sub
Public Shared Sub Print(ByVal text As String, image As Image)
Dim linesarray() = text.Split(New String() _
{Environment.NewLine}, StringSplitOptions.None)
Dim NewBarcode As IDAutomation.Windows.Forms.LinearBarCode.Barcode = New Barcode()
NewBarcode.SymbologyID = Symbologies.Code39
NewBarcode.Code128Set = Code128CharacterSets.A
NewBarcode.RotationAngle = RotationAngles.Zero_Degrees
NewBarcode.RefreshImage()
NewBarcode.Resolution = Resolutions.Screen
NewBarcode.ResolutionCustomDPI = 96
NewBarcode.RefreshImage()
NewBarcode.SaveImageAs("SavedBarcode.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
NewBarcode.Resolution = Resolutions.Printer
For Each line As String In linesarray
Lines.Enqueue(line)
Next
pbImage2.Image = image
prn.Print()
End Sub
Private Shared Sub PrintPageHandler(ByVal sender As Object,
ByVal e As Printing.PrintPageEventArgs)
Dim sf As New StringFormat()
Dim vpos As Single = e.PageSettings.HardMarginY
'String
Do While Lines.Count > 0
Dim line As String = Lines.Dequeue
Dim sz As SizeF = e.Graphics.MeasureString(
line, _myfont, e.PageSettings.Bounds.Size, sf)
Dim rct As New RectangleF(
e.PageSettings.HardMarginX, vpos,
e.PageBounds.Width - e.PageSettings.HardMarginX * 2,
e.PageBounds.Height - e.PageSettings.HardMarginY * 2)
e.Graphics.DrawString(line, _myfont, Brushes.Black, rct)
vpos += sz.Height
If vpos > e.PageSettings.Bounds.Height -
e.PageSettings.HardMarginY * 2 Then
e.HasMorePages = True
Exit Sub
End If
Loop
'IMAGE
Dim rect As New Rectangle(0, 10, 280, 85)
sf.LineAlignment = StringAlignment.Center
Dim printFont10_Normal As New Font("Calibri", 10, FontStyle.Regular, GraphicsUnit.Point)
rect = New Rectangle(0, 10, 280, 15)
e.Graphics.DrawRectangle(Pens.White, rect)
Dim h, w, h2, w2 As Integer
Dim pbImage As New PictureBox
pbImage.Image = Image.FromFile(Application.StartupPath & "\" & "SavedBarcode.Jpeg")
w = Image.FromFile(Application.StartupPath & "\" & "SavedBarcode.Jpeg").Width
h = Image.FromFile(Application.StartupPath & "\" & "SavedBarcode.Jpeg").Height
Dim lPosition As Integer
lPosition = (280 - w) / 2
rect = New Rectangle(50, 25, w, h)
e.Graphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
e.Graphics.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
e.Graphics.DrawImage(pbImage.Image, rect)
e.Graphics.DrawRectangle(Pens.White, rect)
rect = New Rectangle(0, 95, 280, 15)
e.Graphics.DrawRectangle(Pens.White, rect)
End Sub
End Class
Form
Private Sub btn_placebet_Click(sender As Object, e As EventArgs) Handles btn_placebet.Click
Dim strPrint As String
'Dim img As New Bitmap(Label14.Width, Label14.Height)
'fuente = New Font("Code 128", 28)
'Label14.Text = txt_betamount.Text
Dim barcode As Barcode = New Barcode()
Dim foreColor As Color = Color.Black
Dim backColor As Color = Color.Transparent
Dim image As Image = Barcode.Encode(TYPE.CODE128B, txt_betamount.Text, ForeColor, BackColor, 300, 70)
PictureBox1.Image = image
strPrint = "Cash-in & Bet Receipt" & vbCrLf
strPrint = strPrint & "Transaction #: " & " 161089122946" & vbCrLf
strPrint = strPrint & "Date: " & " 2021-01-17 21:47:09" & vbCrLf
strPrint = strPrint & "Arena Name: " & " San Rouque Cockpit Arena" & vbCrLf
strPrint = strPrint & "Event Name: " & " jan-17-2021-event" & vbCrLf
strPrint = strPrint & "Fight No.: " & " 107" & vbCrLf
strPrint = strPrint & "Meron Side: " & " Meron Corner" & vbCrLf
strPrint = strPrint & "Wala Side: " & " Wala Corner" & vbCrLf
strPrint = strPrint & "Casher: " & " ID; 46, Cashier 1 1" & vbCrLf
strPrint = strPrint & "Bet Option: " & " Wala" & vbCrLf
strPrint = strPrint & "Bet Amount: " & " 4,000" & vbCrLf
Print.Print(strPrint, PictureBox1.Image)
End Sub
This is the result
来源:https://stackoverflow.com/questions/66025447/printing-barcode-using-vb-net-class