Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

Print PictureBox1

Hello all,

I want to create a VB button that will print my PictureBox1 withh everything in it.

How can i do this?

Thanks
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

You say "with everything in it"...do you have things being displayed in the Paint() event or possibly some other controls inside the PictureBox?

...or does it only have something in the Image() Property?
Avatar of Wilder1626

ASKER

What i did it to put a picture box in my form and then, put labels, textbox, another picture inside it.
Avatar of andr_gin
andr_gin

A PictureBox only displays an image. Do you mean a GroupBox?
So i should change it to a GroupBox instead of a picturebox?

In VB6 i was using a picture box.
These two controls have completely different functionality:

A PictureBox only displays an image, for example your company logo. It has an Image property and some settings for display (stretch, zoom, center and so on).

A GroupBox is a Container that can hold multiple controls like TextBoxes, ListBoxes, PictureBoxes and so on.
Oh ok, i will start by changing that.

After, i just need to know how i can print that GroupBox1.

Thanks for the information.
*Just to clarify, when you placed controls "in" the PictureBox you were really placing them ON or OVER the PictureBox.  It is technically possible for a PictureBox to "contain" controls but that can only be done at run-time via code.  If you move the PictureBox you'll see that the controls "within" it do NOT move along with it.

In addition to a GroupBox, you could also use a Panel.  To print it, you can tell the container to draw itself with Draw
ToBitmap(), then use that in a PrintDocument via DrawImage():


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bmp As New Bitmap(Panel1.Width, Panel1.Height)
        Panel1.DrawToBitmap(bmp, Panel1.ClientRectangle)

        ' ...Print bmp in PrintDocument with DrawImage()...
    End Sub

Open in new window

Ok,

I will try this at home tonight and i will let you know the result.

Thanks for the help.

Ok, i have change the picturebox for a panel1.

I'm having a problem now with the preview bellow. The page is blank.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        Catch exp As Exception
            MsgBox("An error occurred while trying to load the " & _
                "document for Print Preview. Make sure you currently have " & _
                "access to a printer. A printer must be connected and " & _
                "accessible for Print Preview to work.", MsgBoxStyle.OkOnly, _
                 Me.Panel1.ClientRectangle)
        End Try
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of andr_gin
andr_gin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok thanks, i will try andr_gin code.

I will let you know.
Hello all,

Back again.

I have worked on it since 2 days but i still have an issue.

When i print my Panel1, it print diffrent from what we see.

ex:I have some label, visible = false, but when print, it show etc.

See picture.

What can i do to print what i see on the active form with panel1?




Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Static page As Integer = 1
        Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
        Static maxPages As Integer = 0

        If page = 1 Then
            For Each ctrl As Control In Me.Panel1.Controls
                If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Then
                    ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
                    If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
                End If
            Next
        End If

        For Each ctrl As Control In Me.Panel1.Controls
            If CInt(ctrl.Tag) = page Then
                If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
                    Dim sf As New System.Drawing.StringFormat
                    If TypeOf ctrl Is TextBox Then
                        If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                            sf.Alignment = StringAlignment.Far
                        Else
                            sf.Alignment = StringAlignment.Near
                        End If
                    ElseIf TypeOf ctrl Is Label Then
                        If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then
                            sf.Alignment = StringAlignment.Near
                        ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
                            sf.Alignment = StringAlignment.Far
                        End If
                    End If
                    sf.FormatFlags = StringFormatFlags.NoClip
                    e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)
                ElseIf TypeOf ctrl Is PictureBox Then
                    e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition))
                End If
            End If
        Next

        page += 1
        If page > maxPages Then
            e.HasMorePages = False
            page = 1
            maxPages = 0
        Else
            e.HasMorePages = True
        End If
    End Sub

Open in new window




Print.jpg
Frame.jpg
form-activate.jpg
This is the form4 and also the print preview diffrences
diffrences.jpg
Ok,did some more test.

Now, it is good but i have only half of my panel.
Since i have a scroll baron my panel, i only see the image where i didn't scroll.

How do I print the scrollable panel?




Private Sub btnButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton3.Click
        CapturePanel()
        PrintDocument1.Print()
    End Sub
    Private Sub CapturePanel()
        Dim mygraphics As Graphics = Me.CreateGraphics()
        Dim s As Size = Me.Panel1.Size
        memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
        Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
        Dim dc1 As IntPtr = mygraphics.GetHdc
        Dim dc2 As IntPtr = memoryGraphics.GetHdc
        With Panel1.ClientRectangle
            BitBlt(dc2, 0, 0, .Width, .Height, dc1, Panel1.Location.X, Panel1.Location.Y, 13369376)
        End With

        mygraphics.ReleaseHdc(dc1)
        memoryGraphics.ReleaseHdc(dc2)

    End Sub

Open in new window


Hello all,

I'm coming back again on a way to print a Panel1 that scroll.

How can i do this please?

Thanks
Thanks for your help.