打印图像,文字和边距?

时间:2014-03-07 07:39:33

标签: vb.net printing

我花了几天时间尝试并寻找可以将三者结合起来的Vb.net脚本(打印图像+打印文本+更改边距);但没有运气;

我想:

  • PictureBox1;
  • 中打印图像
  • RichTextBox1打印文字(图片下方或上方);
  • 调整页面页边距;

另外,如果我能在打印前获得上述打印预览的帮助,我感激不尽;

下面是我的启动脚本,它只会操作文本:

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

结束班

1 个答案:

答案 0 :(得分:2)

此答案基于您对想要在图像上绘制文字的评论。

当您将项目绘制到PrintDocument时,如果它们占据图形表面上的相同位置,则首先绘制的项目将位于稍后绘制的任何项目下方。这是关于如何在图像中居中文本的示例。

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

此示例是如何将文本添加到图像的底部。

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