在 vb.net 中保存打印文档或打印预览为图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29629099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save print document or print preview as image in vb.net
提问by user3125081
I want to save a print preview to an image file in vb.net. So far my application generates a print preview with the text the user provides and it prints, but I want to save the printed image to my computer. I have already googled and saw lots of answers, like this
我想将打印预览保存到 vb.net 中的图像文件。到目前为止,我的应用程序使用用户提供的文本生成打印预览并进行打印,但我想将打印的图像保存到我的计算机上。我已经用谷歌搜索并看到了很多答案,就像这样
But somehow it's not working for me. Any help will be appreciate.
但不知何故它对我不起作用。任何帮助将不胜感激。
My current code:
我目前的代码:
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.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim fornt1 As New Font("Arial", 16, FontStyle.Regular)
Dim rect As Rectangle = New Rectangle(New Point(0, 0), PictureBox1.Image.Size)
e.Graphics.DrawImage(PictureBox1.Image, rect) 'Draw Image
e.Graphics.DrawString(RichTextBox1.Text, fornt1, Brushes.LightBlue, 500, 500)
End Sub
End Class
回答by Mousa Alfhaily
I Solved Your Problem.
我解决了你的问题。
Here Is How Your Form Should Look Like:

以下是您的表单的外观:

and here is the Code :
这是代码:
Public Class Form1
Dim BMP As New Drawing.Bitmap(322, 332)
Dim Graph As Graphics = Graphics.FromImage(BMP)
Private Sub PrintBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintBUT.Click
Graph.FillRectangle(Brushes.White, 0, 0, PictureBox1.Width, PictureBox1.Height)
Graph.DrawString(RichTextBox1.Text, RichTextBox1.Font, Brushes.Black, 5, 5)
PictureBox1.Image = BMP
End Sub
Private Sub SaveBUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBUT.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*"
Try
saveFileDialog1.Filter = "JPEG |*.jpeg"
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
Catch ex As Exception
End Try
End Sub
End Class
I hope this code was useful to you. :)
我希望这段代码对你有用。:)

