user4945412 user4945412 - 4 months ago 39
Vb.net Question

printing scrollable panel with image inside in vb

Good Day everyone. i have a problem in printing the image in my scrollable panel. can anyone help me with this?

i wanted to resize the images first (so it can fit the 8.5 by 11" paper) before showing it in print preview then printing it

in my case, i have a form(form1) with a panel (dock=fill) and i wanted to print the images (2 images) inside it. but, the trigger of the printing of image is in the next form

in btnnext(form1)
Form2.Show()
Me.Hide()

and the printing code is in the form2. i am new in vb and i don't have any idea about printing the scrollable panel. i tried different codes in other forums but it didn't print the whole image but only the screenshot of the form. any response will be appreciated., thank you so much




Public Class Form2

Private WithEvents pd As New Printing.PrintDocument
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Using bmp As New Bitmap(Form1.pb1.Width, Form1.pb1.Height)
Form1.panel1.DrawToBitmap(bmp, New Rectangle(0, 0, Form1.pb1.Width, Form1.pb1.Height))
e.Graphics.DrawImage(bmp, e.MarginBounds)
End Using
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PrintDocument1.Print()
End Sub
End Class


-
^it only prints what you can see in the screen. not the whole panel..

Answer

Basically, keep everything the same, but instead of printing the whole first image on the whole page, and print the second image to a ratio of its location and size relative to the MarginBounds

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Using bmp As New Bitmap(Form1.pb1.Width, Form1.pb1.Height)
        Form1.pb1.DrawToBitmap(bmp, New Rectangle(0, 0, Form1.pb1.Width, Form1.pb1.Height))
        'Print pb1 to fill the whole page
        e.Graphics.DrawImage(bmp, e.MarginBounds)
    End Using
    Using bmp2 As New Bitmap(Form1.pb2.Width, Form1.pb2.Height)
        Form1.pb2.DrawToBitmap(bmp2, New Rectangle(0, 0, Form1.pb2.Width, Form1.pb2.Height))

        Dim x as Double = Form1.pb2.Location.X - Form1.pb1.Location.X
        x = x * e.MarginBounds.Width / Form1.pb1.Width
        x = x + e.MarginBounds.X

        Dim y as Double = Form1.pb2.Location.Y - Form1.pb1.Location.Y
        y = y * e.MarginBounds.Height / Form1.pb1.Height
        y = y + e.MarginBounds.Y

        Dim width as Double = Form1.pb2.width
        width = width * e.MarginBounds.Width / Form1.pb1.Width

        Dim height as Double = Form1.pb2.Height
        height = height * e.MarginBounds.Height / Form1.pb1.Height

        e.Graphics.DrawImage(bmp2, New Rectangle(x, y, width, height))
    End Using
End Sub
Comments