How to print out a picture

esposito

Centurion
Joined
Jul 11, 2003
Messages
103
Location
Perugia - Italy
Hello, I know how to print out text on paper in VB.NET but I still have to learn how to print out a picture, e.g. the image loaded in a picturebox.

Can you help me?

TIA
 
Here's a basic text and bitmap print event...I loaded the bitmap image from a file, but you can spec it to a picture box just as easy. This should give you an idea how to proceed anyway.

Visual Basic:
    Private Sub PDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PDoc.PrintPage

        'setup the bmp image
        Dim bmp As Bitmap = New Bitmap(System.Drawing.Image.FromFile(Application.StartupPath & "\Data\R001.bmp"), pnlDrawing.Width, pnlDrawing.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim lbl As Label
        Dim strFormat As New System.Drawing.StringFormat()

        'add the resistor values
        For Each lbl In Me.pnlDrawing.Controls
            If lbl.Text <> Nothing Then
                strFormat.Alignment = StringAlignment.Center
                Dim lblTextF As New RectangleF(lbl.Location.X, lbl.Location.Y, lbl.Width, lbl.Height)
                g.DrawString(lbl.Text, lbl.Font, New SolidBrush(Color.Black), lblTextF, strFormat)

                'add the resistor colored bands
            Else
                Dim lblBandF As New RectangleF(lbl.Location.X, lbl.Location.Y, lbl.Width, lbl.Height)
                lbl.BackgroundImage = lbl.ImageList.Images(lbl.ImageIndex)
                g.DrawImage(lbl.BackgroundImage, lblBandF)
            End If
        Next

        'print bmp
        Dim R As Rectangle
        Dim PWidth, PHeight, PictLeft, PictTop As Integer

        PWidth = bmp.Width
        PHeight = bmp.Height
        With PDoc.DefaultPageSettings.PaperSize
            PictLeft = (PDoc.DefaultPageSettings.PaperSize.Width - PWidth) / 2
            PictTop = 130
        End With

        R = New Rectangle(PictLeft, PictTop, PWidth, PHeight)
        e.Graphics.DrawImage(bmp, R)

        Dim boldFont As New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)

        'Print Header
        Dim TitleF As New System.Drawing.SizeF()
        TitleF = e.Graphics.MeasureString("Resistor color code and value", boldFont)
        e.Graphics.DrawString("Resistor color code and value", boldFont, Brushes.Black, (PDoc.DefaultPageSettings.PaperSize.Width - TitleF.Width) / 2, 100)

        bmp.Dispose()
        boldFont.Dispose()
    End Sub

Hope this helps you a bit
Dan
 
Back
Top