none
虚心请教如何打印Graphics RRS feed

  • 常规讨论

  • 画了几个图形,写了几行字符,可以用Graphics显示在窗体的指定位置。

    现在想把他们发送到打印机进行打印,不知道怎么做。打印的事件过程,用到的方法和属性有哪些?

    2010年11月4日 8:45

全部回复

  • 找到一个例子:

     

    Imports System
    Imports System.Windows.Forms
    Imports System.Drawing
    Imports System.Drawing.Printing
    
    Public Class Form1
     Inherits Form
     Private WithEvents printButton As New Button
     Private WithEvents printDocument1 As New PrintDocument
    
     Public Sub New()
      printButton.Text = "Print Form"
      Me.Controls.Add(printButton)
     End Sub
    
     Dim memoryImage As Bitmap
    
     Private Sub CaptureScreen()
      Dim myGraphics As Graphics = Me.CreateGraphics()
      Dim s As Size = Me.Size
      memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
      Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
      memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
     End Sub
    
     Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _
      ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
      printDocument1.PrintPage
      e.Graphics.DrawImage(memoryImage, 0, 0)
     End Sub
    
     Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As _
      System.EventArgs) Handles printButton.Click
      CaptureScreen()
      printDocument1.Print()
     End Sub
    
     Public Shared Sub Main()
      Application.Run(New Form1())
     End Sub
    End Class
    
    
    其中为什么要这两句呢?起什么作用呢?没有会怎样?
    
      Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
      memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
    
    
    2010年11月5日 3:11
  • 您好

    以您的程式码来说,CopyFromScreen 方法是将目前画面复制下来(可以想像是按Print Screen 键复制画面),然后透过printDocument 进行打印

    http://msdn.microsoft.com/zh-cn/library/system.drawing.graphics.copyfromscreen.aspx

    因此这两行是必要的


    歡迎參觀我的Blog.NET菜鳥自救會
    2010年11月5日 6:11