VB.Net/GUI/Print Preview

Материал из VB Эксперт
Перейти к: навигация, поиск

Print Preview and Print

<source lang="vbnet"> Imports System Imports System.Windows.Forms Imports System.Collections.Generic Imports System.ruponentModel Imports System.Drawing.Imaging Imports System.Drawing Imports System.Drawing.Printing Imports System.Drawing.Drawing2D Public Class MainClass

  Shared Sub Main()
      Dim form1 As Form1 =  new Form1()
      Application.Run(form1)
  End Sub 

End Class

Public Class Form1

   " Display a print preview dialog.
   Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
       dlgPrintPreview.ShowDialog()
   End Sub
   " Display a print dialog.
   Private Sub btnPrintDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDialog.Click
       If dlgPrint.ShowDialog() = Windows.Forms.DialogResult.OK Then
           dlgPrint.Document.Print()
       End If
   End Sub
   " Print now.
   Private Sub btnPrintNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintNow.Click
       pdocGraph.Print()
   End Sub
   " Print a page.
   Private Sub pdocGraph_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdocGraph.PrintPage
       e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)
       Dim picture_rect As New RectangleF(100, 100, 500, 300)
       Dim margin_rect As New RectangleF( _
           e.MarginBounds.X, _
           e.MarginBounds.Y, _
           e.MarginBounds.Width, _
           e.MarginBounds.Height)
       CenterPictureInMargins(e.Graphics, picture_rect, margin_rect)
       " Draw a rectangle 
       e.Graphics.FillRectangle(Brushes.LightGray, picture_rect)
       e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(picture_rect))
       " There are no more pages.
       e.HasMorePages = False
   End Sub
   Private Sub CenterPictureInMargins(ByVal gr As Graphics, ByVal picture_bounds As RectangleF, ByVal margin_bounds As RectangleF)
       gr.ResetTransform()
       Dim dx As Single = _
           margin_bounds.Left - picture_bounds.Left + _
           (margin_bounds.Width - picture_bounds.Width) / 2
       Dim dy As Single = _
           margin_bounds.Top - picture_bounds.Top + _
           (margin_bounds.Height - picture_bounds.Height) / 2
       gr.TranslateTransform(dx, dy)
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.btnPrintNow = New System.Windows.Forms.Button
       Me.btnPrintDialog = New System.Windows.Forms.Button
       Me.btnPrintPreview = New System.Windows.Forms.Button
       Me.dlgPrintPreview = New System.Windows.Forms.PrintPreviewDialog
       Me.pdocGraph = New System.Drawing.Printing.PrintDocument
       Me.dlgPrint = New System.Windows.Forms.PrintDialog
       Me.SuspendLayout()
       "
       "btnPrintNow
       "
       Me.btnPrintNow.Location = New System.Drawing.Point(49, 90)
       Me.btnPrintNow.Name = "btnPrintNow"
       Me.btnPrintNow.Size = New System.Drawing.Size(88, 24)
       Me.btnPrintNow.TabIndex = 11
       Me.btnPrintNow.Text = "Print Now"
       "
       "btnPrintDialog
       "
       Me.btnPrintDialog.Location = New System.Drawing.Point(49, 58)
       Me.btnPrintDialog.Name = "btnPrintDialog"
       Me.btnPrintDialog.Size = New System.Drawing.Size(88, 24)
       Me.btnPrintDialog.TabIndex = 10
       Me.btnPrintDialog.Text = "Print Dialog"
       "
       "btnPrintPreview
       "
       Me.btnPrintPreview.Location = New System.Drawing.Point(49, 26)
       Me.btnPrintPreview.Name = "btnPrintPreview"
       Me.btnPrintPreview.Size = New System.Drawing.Size(88, 24)
       Me.btnPrintPreview.TabIndex = 9
       Me.btnPrintPreview.Text = "Print Preview"
       "
       "dlgPrintPreview
       "
       Me.dlgPrintPreview.AutoScrollMargin = New System.Drawing.Size(0, 0)
       Me.dlgPrintPreview.AutoScrollMinSize = New System.Drawing.Size(0, 0)
       Me.dlgPrintPreview.ClientSize = New System.Drawing.Size(400, 300)
       Me.dlgPrintPreview.Document = Me.pdocGraph
       Me.dlgPrintPreview.Enabled = True
       Me.dlgPrintPreview.Name = "dlgPrintPreview"
       Me.dlgPrintPreview.Visible = False
       "
       "dlgPrint
       "
       Me.dlgPrint.Document = Me.pdocGraph
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(187, 140)
       Me.Controls.Add(Me.btnPrintNow)
       Me.Controls.Add(Me.btnPrintDialog)
       Me.Controls.Add(Me.btnPrintPreview)
       Me.Name = "Form1"
       Me.Text = "CenterPicture"
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents btnPrintNow As System.Windows.Forms.Button
   Friend WithEvents btnPrintDialog As System.Windows.Forms.Button
   Friend WithEvents btnPrintPreview As System.Windows.Forms.Button
   Friend WithEvents dlgPrintPreview As System.Windows.Forms.PrintPreviewDialog
   Friend WithEvents pdocGraph As System.Drawing.Printing.PrintDocument
   Friend WithEvents dlgPrint As System.Windows.Forms.PrintDialog

End Class

      </source>


Print Preview Control

<source lang="vbnet"> Imports System Imports System.Data Imports System.Collections Imports System.Windows.Forms Imports System.Drawing Imports System.Data.SqlClient public class MainClass

  Shared Sub Main()
      Dim form1 As Form = New Form1
      Application.Run(form1)
  End Sub

End Class

Public Class Form1

   Private m_PageNum As Integer = 1
   Private Sub pdocShapes_PrintPage(ByVal sender As System.Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdocShapes.PrintPage
       Select Case m_PageNum
           Case 1  " Page 1. Draw a rectangle in red.
               e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds())
               e.HasMorePages = True
               m_PageNum += 1
           Case 2  " Page 2. Draw a rectangle in green.
               e.Graphics.DrawRectangle(Pens.Green, e.MarginBounds())
               e.HasMorePages = True
               m_PageNum += 1
           Case 3  " Page 3. Draw an rectangle in blue.
               e.Graphics.DrawRectangle(Pens.Blue, e.MarginBounds())
               e.HasMorePages = False
               m_PageNum = 1
       End Select
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.pdocShapes = New System.Drawing.Printing.PrintDocument
       Me.ppvShapes = New System.Windows.Forms.PrintPreviewControl
       Me.SuspendLayout()
       "
       "ppvShapes
       "
       Me.ppvShapes.AutoZoom = False
       Me.ppvShapes.Columns = 3
       Me.ppvShapes.Dock = System.Windows.Forms.DockStyle.Fill
       Me.ppvShapes.Document = Me.pdocShapes
       Me.ppvShapes.Location = New System.Drawing.Point(0, 0)
       Me.ppvShapes.Name = "ppvShapes"
       Me.ppvShapes.Size = New System.Drawing.Size(544, 228)
       Me.ppvShapes.TabIndex = 1
       Me.ppvShapes.Zoom = 0.18509803921568627
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(544, 228)
       Me.Controls.Add(Me.ppvShapes)
       Me.Name = "Form1"
       Me.Text = "UsePrintPreviewControl"
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents pdocShapes As System.Drawing.Printing.PrintDocument
   Friend WithEvents ppvShapes As System.Windows.Forms.PrintPreviewControl

End Class

      </source>


Print Preview Dialog Demo

<source lang="vbnet"> Imports System Imports System.Data Imports System.Collections Imports System.Windows.Forms Imports System.Drawing Imports System.Data.SqlClient public class MainClass

  Shared Sub Main()
      Dim form1 As Form = New Form1
      Application.Run(form1)
  End Sub

End Class

Public Class Form1

   Private m_PageNum As Integer = 1
   Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
       If ppvdlgShapes.ShowDialog() = Windows.Forms.DialogResult.OK Then
           ppvdlgShapes.Document.Print()
       End If
   End Sub
   Private Sub pdocShapes_PrintPage(ByVal sender As System.Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdocShapes.PrintPage
       Select Case m_PageNum
           Case 1  " Page 1. Draw a rectangle in red.
               e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds())
               e.HasMorePages = True
               m_PageNum += 1
           Case 2  " Page 2. Draw a rectangle in green.
               e.Graphics.DrawRectangle(Pens.Green, e.MarginBounds())
               e.HasMorePages = True
               m_PageNum += 1
           Case 3  " Page 3. Draw an rectangle in blue.
               e.Graphics.DrawRectangle(Pens.Blue, e.MarginBounds())
               e.HasMorePages = False
               m_PageNum = 1
       End Select
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.btnPreview = New System.Windows.Forms.Button
       Me.pdocShapes = New System.Drawing.Printing.PrintDocument
       Me.ppvdlgShapes = New System.Windows.Forms.PrintPreviewDialog
       Me.SuspendLayout()
       "
       "btnPreview
       "
       Me.btnPreview.Location = New System.Drawing.Point(112, 48)
       Me.btnPreview.Name = "btnPreview"
       Me.btnPreview.Size = New System.Drawing.Size(64, 24)
       Me.btnPreview.TabIndex = 1
       Me.btnPreview.Text = "Preview"
       "
       "ppvdlgShapes
       "
       Me.ppvdlgShapes.AutoScrollMargin = New System.Drawing.Size(0, 0)
       Me.ppvdlgShapes.AutoScrollMinSize = New System.Drawing.Size(0, 0)
       Me.ppvdlgShapes.ClientSize = New System.Drawing.Size(400, 300)
       Me.ppvdlgShapes.Document = Me.pdocShapes
       Me.ppvdlgShapes.Enabled = True
       Me.ppvdlgShapes.Icon = New Icon("test.ico")
       Me.ppvdlgShapes.Name = "ppvdlgShapes"
       Me.ppvdlgShapes.Visible = False
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(292, 142)
       Me.Controls.Add(Me.btnPreview)
       Me.Name = "Form1"
       Me.Text = "UsePrintPreviewDialog"
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents btnPreview As System.Windows.Forms.Button
   Friend WithEvents pdocShapes As System.Drawing.Printing.PrintDocument
   Friend WithEvents ppvdlgShapes As System.Windows.Forms.PrintPreviewDialog

End Class

      </source>


Print Preview, print dialog for a custome Pie

<source lang="vbnet"> Imports System Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Drawing.Printing Public Class MainClass

   Public Shared Sub Main()
      Application.Run(New Form1())
   End Sub

End Class

Public Class Form1

 Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
 Public Sub New()
   MyBase.New()
   "This call is required by the Windows Form Designer.
   InitializeComponent()
 End Sub
 "Form overrides dispose to clean up the component list.
 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
   If disposing Then
     If Not (components Is Nothing) Then
       components.Dispose()
     End If
   End If
   MyBase.Dispose(disposing)
 End Sub
 "Required by the Windows Form Designer
 Private components As System.ruponentModel.IContainer
 "NOTE: The following procedure is required by the Windows Form Designer
 "It can be modified using the Windows Form Designer.  
 "Do not modify it using the code editor.
 Friend WithEvents cmdQuit As System.Windows.Forms.Button
 Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
 Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
 Friend WithEvents mnuPreview As System.Windows.Forms.MenuItem
 Friend WithEvents mnuPrint As System.Windows.Forms.MenuItem
 Friend WithEvents mnuSetup As System.Windows.Forms.MenuItem
 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
   Me.cmdQuit = New System.Windows.Forms.Button()
   Me.MainMenu1 = New System.Windows.Forms.MainMenu()
   Me.mnuFile = New System.Windows.Forms.MenuItem()
   Me.mnuSetup = New System.Windows.Forms.MenuItem()
   Me.mnuPreview = New System.Windows.Forms.MenuItem()
   Me.mnuPrint = New System.Windows.Forms.MenuItem()
   Me.SuspendLayout()
   "
   "cmdQuit
   "
   Me.cmdQuit.Location = New System.Drawing.Point(264, 280)
   Me.cmdQuit.Name = "cmdQuit"
   Me.cmdQuit.Size = New System.Drawing.Size(64, 32)
   Me.cmdQuit.TabIndex = 1
   Me.cmdQuit.Text = "Quit"
   "
   "MainMenu1
   "
   Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile})
   "
   "mnuFile
   "
   Me.mnuFile.Index = 0
   Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuSetup, Me.mnuPreview, Me.mnuPrint})
   Me.mnuFile.Text = "File"
   "
   "mnuSetup
   "
   Me.mnuSetup.Index = 0
   Me.mnuSetup.Text = "Page Setup"
   "
   "mnuPreview
   "
   Me.mnuPreview.Index = 1
   Me.mnuPreview.Text = "Print Preview"
   "
   "mnuPrint
   "
   Me.mnuPrint.Index = 2
   Me.mnuPrint.Text = "Print"
   "
   "Form1
   "
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(342, 323)
   Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdQuit})
   Me.MaximizeBox = False
   Me.Menu = Me.MainMenu1
   Me.MinimizeBox = False
   Me.Name = "Form1"
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
   Me.Text = "Form1"
   Me.ResumeLayout(False)
 End Sub
  1. End Region
   Dim Pv As PrintPreviewDialog = New PrintPreviewDialog()
   Dim Ps As PageSetupDialog = New PageSetupDialog()
   Dim Pd As PrintDocument = New PrintDocument()
   Dim Pr As PrintDialog = New PrintDialog()
 Private Sub Form1_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles MyBase.Load
   Pd.DocumentName = "My New Document"
   Pv.Document = Pd
   Ps.Document = Pd
   Pr.Document = Pd
   AddHandler Pd.PrintPage, New PrintPageEventHandler(AddressOf Me.pd_Print)
 End Sub
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
   DrawIt(e.Graphics)
 End Sub
 Private Sub DrawIt(ByVal G As Graphics)
   G.SmoothingMode = SmoothingMode.AntiAlias
   Dim P1 As Pen = New Pen(Brushes.Violet, 5)
   G.DrawPie(P1, 0, 0, 250, 250, 0, 350)
 End Sub
 Private Sub pd_Print(ByVal sender As Object, ByVal e As PrintPageEventArgs)
   DrawIt(e.Graphics)
 End Sub
 Private Sub mnuSetup_Click(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles mnuSetup.Click
   Ps.ShowDialog()
   Pd.DefaultPageSettings = Ps.PageSettings
   Pd.PrinterSettings = Ps.PrinterSettings
 End Sub
 Private Sub mnuPreview_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                             Handles mnuPreview.Click
   Pv.WindowState = FormWindowState.Maximized
   Pv.ShowDialog()
 End Sub
 Private Sub cmdQuit_Click(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles cmdQuit.Click
   Me.Dispose()
 End Sub
 Private Sub mnuPrint_Click(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                           Handles mnuPrint.Click
   If Pr.ShowDialog() = DialogResult.OK Then
     Pd.Print()
   End If
 End Sub

End Class

      </source>


Print preview your document before print

<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.IO Imports System.Xml.Serialization Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Drawing.Printing

Public Class MainClass

   Shared Sub Main()
      Dim form1 As Form = New Form1()
      Application.Run(form1) 
    End Sub

End Class


Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
   Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
   Friend WithEvents btnPrint As System.Windows.Forms.Button
   Friend WithEvents btnPreview As System.Windows.Forms.Button
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.Container
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
       Me.btnPrint = New System.Windows.Forms.Button()
       Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument()
       Me.PictureBox1 = New System.Windows.Forms.PictureBox()
       Me.btnPreview = New System.Windows.Forms.Button()
       Me.SuspendLayout()
       "
       "btnPrint
       "
       Me.btnPrint.Location = New System.Drawing.Point(84, 232)
       Me.btnPrint.Name = "btnPrint"
       Me.btnPrint.Size = New System.Drawing.Size(96, 32)
       Me.btnPrint.TabIndex = 0
       Me.btnPrint.Text = "Print"
       "
       "PrintDocument1
       "
       "
       "PictureBox1
       "
       Me.PictureBox1.Image = New System.Drawing.Bitmap("figure2.bmp")
       Me.PictureBox1.Location = New System.Drawing.Point(68, 8)
       Me.PictureBox1.Name = "PictureBox1"
       Me.PictureBox1.Size = New System.Drawing.Size(264, 200)
       Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
       Me.PictureBox1.TabIndex = 1
       Me.PictureBox1.TabStop = False
       "
       "btnPreview
       "
       Me.btnPreview.Location = New System.Drawing.Point(212, 232)
       Me.btnPreview.Name = "btnPreview"
       Me.btnPreview.Size = New System.Drawing.Size(96, 32)
       Me.btnPreview.TabIndex = 2
       Me.btnPreview.Text = "Print Preview"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
       Me.ClientSize = New System.Drawing.Size(400, 268)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnPreview, Me.PictureBox1, Me.btnPrint})
       Me.Name = "Form1"
       Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
       Me.Text = "Demonstration of Printing and print preview"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   End Sub
   Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
 ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
 Handles PrintDocument1.PrintPage
       e.PageSettings.Margins = New System.Drawing.Printing.Margins(250, 250, 250, 250)
       Dim g As Graphics
       g = e.Graphics
       g.DrawImage(PictureBox1.Image, e.MarginBounds.Left, e.MarginBounds.Top)
       g.Dispose()
       e.HasMorePages = False
   End Sub
   Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
       Dim PrintDialog1 As New PrintDialog()
       PrintDialog1.Document = PrintDocument1
       If PrintDialog1.ShowDialog() = DialogResult.OK Then
           PrintDocument1.Print()
       End If
   End Sub
   Private Sub btnPreview_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnPreview.Click
       Dim PrintPreviewDialog1 As New PrintPreviewDialog()
       PrintPreviewDialog1.Document = PrintDocument1
       If PrintPreviewDialog1.ShowDialog() = DialogResult.OK Then
           PrintDocument1.Print()
       End If
   End Sub

End Class

      </source>


Simple Demo for Print Preview Control

<source lang="vbnet"> Imports System Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Globalization Imports System.Text Imports System.Collections Imports System.Drawing.Printing Public Class MainClass

   Shared Sub Main()
       
       Dim myform As Form = New CustomPrintPreviewDialog()
       Application.Run(myform)
   End Sub

End Class


Public Class CustomPrintPreviewDialog

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   Friend WithEvents printPreviewControl1 As System.Windows.Forms.PrintPreviewControl
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.printPreviewControl1 = New System.Windows.Forms.PrintPreviewControl()
       Me.SuspendLayout()
       "
       "printPreviewControl1
       "
       Me.printPreviewControl1.AutoZoom = False
       Me.printPreviewControl1.Dock = System.Windows.Forms.DockStyle.Fill
       Me.printPreviewControl1.Name = "printPreviewControl1"
       Me.printPreviewControl1.Size = New System.Drawing.Size(376, 398)
       Me.printPreviewControl1.TabIndex = 1
       Me.printPreviewControl1.UseAntiAlias = True
       Me.printPreviewControl1.Zoom = 0.30000001192092896
       "
       "CustomPrintPreviewDialog
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(376, 398)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.printPreviewControl1})
       Me.Name = "CustomPrintPreviewDialog"
       Me.Text = "CustomPrintPreviewDialog"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub printPreviewControl1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles printPreviewControl1.Click
       If (Control.ModifierKeys And Keys.Shift) = 0 Then
           printPreviewControl1.Zoom = printPreviewControl1.Zoom * 2.0
       Else
           printPreviewControl1.Zoom = printPreviewControl1.Zoom / 2.0
       End If
   End Sub
   Public Property Document() As PrintDocument
       Get
           Return printPreviewControl1.Document
       End Get
       Set(ByVal Value As PrintDocument)
           printPreviewControl1.Document = Value
       End Set
   End Property

End Class

      </source>