Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1329
  • Last Modified:

How do I print a bitmap from VB.NET 2005?

How do I print a bitmap image from VB.NET 2005?  I want to print it without any dialogues so that when the user hits a button in the Windows application, the image starts printing.
0
stev0931
Asked:
stev0931
  • 2
2 Solutions
 
jrschererCommented:
Hi stevo931
Try This. I did'nt test it, but it should work.

Imports System.IO
......

    Dim img As Image
    Dim PrintDocument1 As New ProntDocument
    Dim FileSpec As String

    ' Print the ImageFile onto the Default Printer with Default Settings
    Private Sub PrintPage(Byval ImageFile As String)

      If ImageFile.Length = 0 then Exit Sub
      FileSpec = ImageFile
          PrintDocument1.Print()
      End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            If File.Exists(FileSpec) Then ' make sure the file exists
                Try
                    img = Image.FromFile(FileSpec)
                    e.Graphics.DrawImage(img)
                Catch ex As Exception
                     .......
                    Exit Sub
                End Try
            End If
    End Sub

Don't forget to dispose off img when you leave

..
If img Isnot Nothing then
  img.Dispose
End If
..

Jack.net
0
 
cmjwebservicesCommented:
Here's another way you could do it (ported from vb.net 2003, but works all the same in 2005; just delete the duplicate items and all will run well).  Just add a new form and paste the following code in:

Option Strict On
Option Explicit On

#Region " Imports "

Imports System.IO
Imports System.Drawing.Printing

#End Region

Public Class frmPrintImage
    Inherits System.Windows.Forms.Form

#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.ComponentModel.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 pctImage As System.Windows.Forms.PictureBox
    Friend WithEvents btnPrint As System.Windows.Forms.Button
    Friend WithEvents PrintDialog1 As System.Windows.Forms.PrintDialog
    Friend WithEvents btnClose As System.Windows.Forms.Button
    Friend WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
    Friend WithEvents PageSetupDialog1 As System.Windows.Forms.PageSetupDialog
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmPrintImage))
        Me.pctImage = New System.Windows.Forms.PictureBox
        Me.btnPrint = New System.Windows.Forms.Button
        Me.PrintDialog1 = New System.Windows.Forms.PrintDialog
        Me.btnClose = New System.Windows.Forms.Button
        Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog
        Me.PageSetupDialog1 = New System.Windows.Forms.PageSetupDialog
        CType(Me.pctImage, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'pctImage
        '
        Me.pctImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pctImage.Dock = System.Windows.Forms.DockStyle.Fill
        Me.pctImage.Image = CType(resources.GetObject("pctImage.Image"), System.Drawing.Image)
        Me.pctImage.Location = New System.Drawing.Point(0, 0)
        Me.pctImage.Name = "pctImage"
        Me.pctImage.Size = New System.Drawing.Size(800, 600)
        Me.pctImage.TabIndex = 0
        Me.pctImage.TabStop = False
        '
        'btnPrint
        '
        Me.btnPrint.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnPrint.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.btnPrint.Location = New System.Drawing.Point(648, 568)
        Me.btnPrint.Name = "btnPrint"
        Me.btnPrint.Size = New System.Drawing.Size(72, 24)
        Me.btnPrint.TabIndex = 1
        Me.btnPrint.Text = "Print"
        '
        'btnClose
        '
        Me.btnClose.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.btnClose.Location = New System.Drawing.Point(720, 568)
        Me.btnClose.Name = "btnClose"
        Me.btnClose.Size = New System.Drawing.Size(72, 24)
        Me.btnClose.TabIndex = 2
        Me.btnClose.Text = "Close"
        '
        'PrintPreviewDialog1
        '
        Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
        Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
        Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
        Me.PrintPreviewDialog1.Enabled = True
        Me.PrintPreviewDialog1.Icon = CType(resources.GetObject("PrintPreviewDialog1.Icon"), System.Drawing.Icon)
        Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
        Me.PrintPreviewDialog1.Visible = False
        '
        'frmTestPattern
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(800, 600)
        Me.Controls.Add(Me.btnClose)
        Me.Controls.Add(Me.btnPrint)
        Me.Controls.Add(Me.pctImage)
        Me.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.MaximizeBox = False
        Me.MaximumSize = New System.Drawing.Size(800, 600)
        Me.MinimizeBox = False
        Me.MinimumSize = New System.Drawing.Size(800, 600)
        Me.Name = "frmTestPattern"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Test Pattern"
        CType(Me.pctImage, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region " Form_Load "

    Private Sub frmTestPattern_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        pctImage.SizeMode = PictureBoxSizeMode.Normal
    End Sub 'frmTestPattern_Load

#End Region

#Region " Buttons "

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub 'btnClose_Click

#End Region

#Region " Print "

    Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        e.Graphics.DrawImage(Me.pctImage.Image, 0, 0)
    End Sub 'OnPrintPage

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim pd As New System.Drawing.Printing.PrintDocument
        AddHandler pd.PrintPage, AddressOf OnPrintPage
        pd.Print()
    End Sub 'btnPrint_Click

#End Region

End Class 'frmPrintImage
0
 
cmjwebservicesCommented:
In this example I just added a default image to the picturebox on the form (in this case a windows wallpaper).  Any image will suffice though.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now