Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to print PictureBox.Image with String Using Class

Status
Not open for further replies.

ren123

Student
Feb 3, 2021
1
I hope the title is enough to understand my problem, I just want that if the button is click, i want to print an image with some text using class,

just like this in the picture below (example print)

Capture_dcyghe.png


This is my Print.vb (Class)

Code:
Public Class Print
    Private Shared Lines As New Queue(Of String)
    Private Shared _myfont As Font
    Private Shared prn As Printing.PrintDocument
    Private Shared G As Graphics
    Public Shared Str As String
    Private Shared PhotoBox As New Rectangle(5, 5, 150, 200)
    Public Shared Photo As Image

    Shared Sub New()
        _myfont = New Font("Courier New",
                  8, 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)

        'EmpPhoto = Photo
        For Each line As String In linesarray
            Lines.Enqueue(line)
        Next
        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

        If Photo IsNot Nothing Then
            G.DrawImage(Photo, New Rectangle(PhotoBox.X + 1, PhotoBox.Y + 1, PhotoBox.Width - 1, PhotoBox.Height - 1))
        End If
        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
    End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor