2015-04-14 113 views
2

我想將打印預覽保存到vb.net中的圖像文件。 到目前爲止,我的應用程序使用用戶提供的文本生成打印預覽並打印,但我想將打印的圖像保存到我的計算機上。 我已經使用Google,並看到很多答案,如this將打印文檔或打印預覽保存爲vb.net中的圖像

但不知何故,它不適合我。任何幫助將不勝感激。

我當前的代碼:

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 

回答

0

我解決你的問題。

這裏是如何使你的窗體應該類似於: enter image description here

這裏是代碼:

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 

我希望這些代碼對你有用。 :)