Solved

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

Posted on 2006-06-16
3
1,283 Views
Last Modified: 2011-10-03
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
Comment
Question by:stev0931
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
jrscherer earned 75 total points
ID: 16925612
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
 
LVL 2

Assisted Solution

by:cmjwebservices
cmjwebservices earned 50 total points
ID: 16925678
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
 
LVL 2

Expert Comment

by:cmjwebservices
ID: 16925679
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now