0% found this document useful (0 votes)
10 views

Text

Vb.net
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Text

Vb.net
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Imports System.Drawing.

Printing
Imports ZXing

Public Class BarcodeGeneratorForm


Dim barcodeWriter As New BarcodeWriter()
Dim printDocument As New PrintDocument()
Dim barcodeSizeInInches As New SizeF(1, 0.5) ' Set the desired size in inches
Dim includeHumanReadable As Boolean = True ' Set to False if you don't want
human-readable text

Private Sub BarcodeGeneratorForm_Load(sender As Object, e As EventArgs) Handles


MyBase.Load
' Set up barcode properties
barcodeWriter.Format = BarcodeFormat.CODE_128 ' You can choose other
formats as needed

' Set up print document


AddHandler printDocument.PrintPage, AddressOf PrintPageHandler

' Set up mouse events for resizing


AddHandler PictureBox1.MouseDown, AddressOf PictureBoxMouseDown
AddHandler PictureBox1.MouseMove, AddressOf PictureBoxMouseMove
AddHandler PictureBox1.MouseUp, AddressOf PictureBoxMouseUp
End Sub

Private Sub GenerateBarcodesButton_Click(sender As Object, e As EventArgs)


Handles GenerateBarcodesButton.Click
' Show print dialog to choose a printer
If PrintDialog1.ShowDialog() = DialogResult.OK Then
' Print barcodes using data from TextBox
printDocument.PrinterSettings = PrintDialog1.PrinterSettings
PrintDocument.Print()
End If
End Sub

Private Sub PrintPageHandler(sender As Object, e As PrintPageEventArgs)


' Generate and print barcode based on TextBox data
GenerateAndPrintBarcode(BarcodeDataTextBox.Text, e)
End Sub

Private Sub GenerateAndPrintBarcode(data As String, e As PrintPageEventArgs)


' Calculate barcode dimensions based on printer resolution and desired size
Dim dpiX As Single = e.Graphics.DpiX
Dim dpiY As Single = e.Graphics.DpiY
Dim barcodeWidth = CInt(barcodeSizeInInches.Width * dpiX)
Dim barcodeHeight = CInt(barcodeSizeInInches.Height * dpiY)

' Create barcode image


barcodeWriter.Options = New ZXing.Common.EncodingOptions With {
.Width = barcodeWidth,
.Height = barcodeHeight
}
Dim barcodeBitmap As Bitmap = barcodeWriter.Write(data)

' Draw barcode on print page


e.Graphics.DrawImage(barcodeBitmap, New Point(10, 10)) ' Adjust the
coordinates as needed

' Draw human-readable text below the barcode if required


If includeHumanReadable Then
Dim font As New Font("Arial", 10) ' Adjust font and size as needed
Dim textPosition As New Point(10, 10 + barcodeHeight + 5) ' Adjust the
Y coordinate for spacing
e.Graphics.DrawString(data, font, Brushes.Black, textPosition)
End If

' Add your logic for additional elements on the print page if necessary
End Sub

Private isResizing As Boolean = False


Private resizeStartPoint As Point
Private initialSize As Size

Private Sub PictureBoxMouseDown(sender As Object, e As MouseEventArgs)


If e.Button = MouseButtons.Left Then
isResizing = True
resizeStartPoint = e.Location
initialSize = PictureBox1.Size
End If
End Sub

Private Sub PictureBoxMouseMove(sender As Object, e As MouseEventArgs)


If isResizing Then
Dim deltaX As Integer = e.X - resizeStartPoint.X
Dim deltaY As Integer = e.Y - resizeStartPoint.Y
PictureBox1.Size = New Size(initialSize.Width + deltaX,
initialSize.Height + deltaY)
End If
End Sub

Private Sub PictureBoxMouseUp(sender As Object, e As MouseEventArgs)


If e.Button = MouseButtons.Left Then
isResizing = False
End If
End Sub

' Add your logic for additional UI elements, barcode data input, and error
checking

End Class

You might also like