Abbas1999
Abbas1999

Reputation: 395

Printing Image, Text and Margins?

I spent few days trying and looking for Vb.net script that can combine the three (Printing Image + Printing Text + changing Margins); but no luck;

I want to:

Also, I appreciate it if I can get help to Print-Preview the above before printing;

Below is my start up script that will only manipulate the text:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    printDocument1.Print()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    printPreviewDialog1.Document = printDocument1
    printPreviewDialog1.ShowDialog()
End Sub

Private Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.PrintPage
    Dim fnt_t As Font
    fnt_t = RichTextBox1.Font
    e.Graphics.DrawString(RichTextBox1.Text, fnt_t, New SolidBrush(Color.Black), 10, 10)
End Sub

End Class

Upvotes: 2

Views: 4486

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

This answer is based on your comment on wanting to draw text on your image.

When you are drawing items to the PrintDocument the one that is drawn first will be underneath any item that is drawn later, if they are occupying the same location on the Graphic surface. This is an example on how to center text in the Image.

Private Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage     
    Dim fnt_t As Font = RichTextBox1.Font 'Set your font
    Dim rect As Rectangle = New Rectangle(New Point(0, 0), PictureBox1.Image.Size) ' used to give a common reference
    Dim fmt As StringFormat = New StringFormat()
    fmt.Alignment = StringAlignment.Center 'Horizontal Centering
    fmt.LineAlignment = StringAlignment.Center 'Vertical Centering
    e.Graphics.DrawImage(PictureBox1.Image, rect) 'Draw Image
    e.Graphics.DrawString(RichTextBox1.Text, fnt_t, New SolidBrush(Color.White), rect, fmt) 'Draw Text
End Sub

enter image description here

And this example is how to add text to the bottom of the image.

Private Sub printDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim fnt_t As Font = RichTextBox1.Font 'Set your font
    Dim rect As Rectangle = New Rectangle(New Point(0, 0), PictureBox1.Image.Size) ' get the size of the image
    Dim lineheight As Integer = TextRenderer.MeasureText(RichTextBox1.Text, fnt_t).Height ' Measure to find height of text
    Dim stringRect As Rectangle = New Rectangle(0, rect.Bottom - lineheight, rect.Width, lineheight) 'Determine rectangle for Text 
    Dim fmt As StringFormat = New StringFormat() 'Tell it to center Text in its rectangle 
    fmt.Alignment = StringAlignment.Center 'Center it 
    e.Graphics.DrawImage(PictureBox1.Image, rect) 'Draw Image
    e.Graphics.DrawString(RichTextBox1.Text, fnt_t, New SolidBrush(Color.White), stringRect, fmt) 'Draw Text
End Sub

enter image description here

Upvotes: 3

Related Questions