print full picture after graphics paint

ridhwaans

Active member
Joined
Jun 1, 2012
Messages
34
Programming Experience
3-5
I've got a tabcontrol with multiple tab pages each containing a different picturebox
these pictureboxes have large images so the tab page is scrollable
when the tab page is selected, and the picturebox is displayed, a paint event triggers and e.graphics are drawn upon the picturebox image

now, when a print button is clicked, I am trying to resize the picturebox to fit page and print the full picturebox (the whole image, including the paint graphics and the non visible portion (scrollable))


The code below resizes and fits the picturebox (called picToPrint) to the actual page, and prints the image, but the e.graphics dont resize and dont print

VB.NET:
Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click

        Dim ret As Windows.Forms.DialogResult
        ret = PrintDialog1.ShowDialog()
        If ret = Windows.Forms.DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            PrintDocument1.Print()
        End If
    End Sub



    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim siza As System.Drawing.Size
        Dim Rctb As System.Drawing.Rectangle
        Dim factorW As Single
        Dim factorH As Single
        Dim factor As Single
        factorW = PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Width / picToPrint.Size.Width
        factorH = PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Height / picToPrint.Size.Height
        If factorH < factorW Then
            factor = factorH
        Else
            factor = factorW
        End If
        siza.Height = picToPrint.Size.Height * factor
        siza.Width = picToPrint.Size.Width * factor

        e.Graphics.DrawImage(picToPrint.Image, 0, 0, siza.Width, siza.Height)

        Rctb.X = picReference.Left * factor
        Rctb.Y = picReference.Top * factor
        Rctb.Width = picReference.Width * factor
        Rctb.Height = picReference.Height * factor
        e.Graphics.DrawImage(picReference.Image, Rctb)

    End Sub


I've tried printing via the PrintForm control from the VB PowerPack, but I only want to print the picturebox inside the tabpage, not the entire form

VB.NET:
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

I would have to resize the graphics and repaint the resized image again before printing it

please advise
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,617
Location
Norway
Programming Experience
10+
print the full picturebox (the whole image, including the paint graphics and the non visible portion (scrollable))
What you paint to the control is not painted to the image the control displays, it is painted directly to the screen (and repainted each Paint event). So if you want to print that you have to draw that to PrintDocument as well.

There exist one other option, and that is the Control.DrawToBitmap method. If you use that with your PictureBox control you can draw what it displays (the whole control, also outside scrolling area) to a Bitmap, which would include invoking Paint event so that it draws the dynamic parts also, then you can print that Bitmap.
 

ridhwaans

Active member
Joined
Jun 1, 2012
Messages
34
Programming Experience
3-5
What you paint to the control is not painted to the image the control displays, it is painted directly to the screen (and repainted each Paint event). So if you want to print that you have to draw that to PrintDocument as well.

There exist one other option, and that is the Control.DrawToBitmap method. If you use that with your PictureBox control you can draw what it displays (the whole control, also outside scrolling area) to a Bitmap, which would include invoking Paint event so that it draws the dynamic parts also, then you can print that Bitmap.

Thank you! The control.drawtobitmap method is what I needed
 
Top Bottom