Print image in PictureBox

smiles

Well-known member
Joined
May 21, 2008
Messages
47
Programming Experience
Beginner
Hi, I did a search and mimic the code from here http://www.vbdotnetforums.com/windows-forms/20808-how-print-part-picturebox-visual-basic-net.html
and it warns "Document does not contain any pages" :confused:
What I did wrong ?
VB.NET:
...
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim Item As System.IO.FileInfo
        Dim Name As String
        Item = ListBox1.SelectedItem
        Name = Item.FullName
        original = Image.FromFile(Name)
        PictureBox1.Image = original
    End Sub
#Region "Printing"
    Private original As Image
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        e.Graphics.DrawImage(original, New Point(0, 0))
    End Sub
    Private Sub PrintToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
        PrintPreviewDialog1.ShowDialog()
    End Sub
#End Region
...
Thanks !!!
 
Have you set the Document property of the PrintPreviewDialog?
 
Have you set the Document property of the PrintPreviewDialog?
I forgot that :eek:
But it fulfills the paper cause of New point(0,0), could you give me a hint to have the image scale and place fine in the printed paper with padding at four side (like you follow Photo Printing Wizard while viewing image by Microsoft Photo Office Manager or Windows Picture and Fax Viewer) :confused:
 
You have many more options for Graphics.DrawImage Method, several for which you can specify an exact Rectangle the Image will be scaled and drawn into.
 
Hi just back and continue with this, thanks for your hint, I get it work already :D:D:D
VB.NET:
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim destRect As Rectangle
        Dim topLeftPoint As Point = New Point(15, 15)
        Dim drawSize As Size
        Dim imgRate As Single = original.Height / original.Width
        Dim stanRate As Single = 1.298
        If imgRate > stanRate Then
            drawSize.Height = 1065
            drawSize.Width = CInt(1065 / imgRate)
        ElseIf imgRate < stanRate Then
            drawSize.Height = CInt(820 * imgRate)
            drawSize.Width = 820
        End If
        If ((original.Width <= 820) And (original.Height <= 1065)) Then
            topLeftPoint = New Point((820 - original.Width) / 2 + 15, (1065 - original.Height) / 2 + 15)
            drawSize.Height = original.Height
            drawSize.Width = original.Width
        End If
        destRect = New Rectangle(topLeftPoint, drawSize)
        'e.Graphics.DrawImage(original, New Point(0, 0))
        e.Graphics.DrawImage(original, destRect)
    End Sub
 
Help me with another problem

Hi, I did a search and mimic the code from here http://www.vbdotnetforums.com/window...basic-net.html
The sample code above allow to print a specify area but in case the image in pictureBox is in normal sizemode, but what if the image is very big :confused:
So I try to scale it into smaller size adapt to my view, try this code
VB.NET:
...
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim Item As System.IO.FileInfo
        Dim Name As String
        Item = ListBox1.SelectedItem
        Name = Item.FullName
        original = Image.FromFile(Name)
        PictureBox1.Image = original
    End Sub
#Region "Printing"
    Private original As Image
    Dim bmpSource As New Bitmap(original)
    Dim bmpDest As New Bitmap(CInt(bmpSource.Width * 500 / bmpSource.Height), 500)
    Dim graDest As Graphics = Graphics.FromImage(bmpDest)
    graDest.DrawImage(bmpSource, 0, 0, bmpDest.Width + 1, bmpDest.Height + 1)
...
It underlines graDest and say Declaration Expected :confused:
I get code from this VB Helper: HowTo: Resize an image in VB .NET
Thanks !!!
 
Back
Top