0

i will describe my problem as much possible. I have created a windows form in vb.net, having a panel, and this panel contains a canvas, the canvas has some drawn content, i want to print the canvas to pdf or whatever, my problem is not with the printer but in sending the canvas to be printed, here's some code i tried but i believe its not okay, it prints an empty page. here's some code (code is initiated by printdocument1.Print() by click of a button):

    Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles printdocument1.PrintPage
    Dim b As New Bitmap(pnlC.Width, pnlC.Height)
    pnlC.DrawToBitmap(b, pnlC.ClientRectangle)
    e.Graphics.DrawImage(b, New Point(0, 0))
    For Each ctrl As Control In pnlC.Controls
        If TypeOf ctrl Is Canvas Then
            ctrl.Tag = Int((ctrl.Top + ctrl.Height) / printdocument1.DefaultPageSettings.Bounds.Height) + 1
            e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - StartPosition))
        End If
    Next
    Dim grp As Graphics = pnlC.CreateGraphics()
    Dim pnlSize As Size = pnlC.Size
    bitmap = New Bitmap(pnlC.Width, pnlC.Height, grp)
    grp = Graphics.FromImage(bitmap)
    Dim panelLocation As Point = PointToScreen(pnlC.Location)
    grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, pnlSize)
    e.HasMorePages = False
End Sub

some code of whats inside the canvas:

   Protected Overrides Sub drawNode(n As Node, iPosition As AbstractVector)
    Dim pos As Point = GraphToScreen(TryCast(iPosition, FDGVector2))
    Dim canvas As Graphics = __graphicsProvider()
    Dim r As Single = n.Data.radius

    SyncLock canvas
        If r = 0! Then
            r = If(n.Data.Neighborhoods < 30, n.Data.Neighborhoods * 9, n.Data.Neighborhoods * 7)
            r = If(r = 0, 20, r)
        End If

        Dim pt As New Point(pos.X - r / 2, pos.Y - r / 2)
        Dim rect As New Rectangle(pt, New Size(r, r))

        Call canvas.FillPie(n.Data.Color, rect, 0, 360)

        'Call canvas.DrawImage(n.Data.image, rect)
        Call canvas.DrawString("test", New Font("Times New Roman", 12), New SolidBrush(Color.Black), pt)
    End SyncLock
End Sub
Yuza
  • 1
  • 2
  • [How to print hidden and visible content of a Container with ScrollBars](https://stackoverflow.com/a/57309095/7444103). Get that class, pass the Panel container to it, see that it returns a Bitmap, print that Bitmap where you see fit. – Jimi Sep 02 '20 at 11:10
  • it didnt work, it went through the whole process and it ordered the paint function but still it returned a blank page. thanks for trying, maybe im missing something crucial here. – Yuza Sep 02 '20 at 12:36
  • Is the *Canvas* your PictureBox? Are you drawing something onto its surface? If so, how? Show that code (if you're using `CreateGraphics()` for drawing, don't show that code, I don't want to see it :) Just kidding (no, I'm not, but I'll see it anyway). – Jimi Sep 02 '20 at 12:43
  • the code of drawing is set across multiple classes, forms, and projects, it all comes down to one solution, the canvas draws nodes connected to each other, no im not using CreateGraphics(), hold ill share a sub – Yuza Sep 02 '20 at 12:50
  • @Jimi i added to the post a way of drawing into the canvas – Yuza Sep 02 '20 at 12:58
  • Yes, well, what is a `__graphicsProvider()`? Where does it come from? What creates it? When is it created? Is it available when you DrawToBitmap the Panel container? And the Data, is it available at that point? You'll have to explain what you're dealing with. – Jimi Sep 02 '20 at 13:03
  • graphics provider is a func of graphics, found in metadata, but the rest is available yes, i can cancel its availability anytime, but since all nodes and connections are already drawn in the canvas, which what i want to print, it will still try to draw after calling the print, but if it is stopped, it prints blank in both ways, and call the print normally and works fine, but blank – Yuza Sep 02 '20 at 13:28
  • Read the my previous comment. I need that information. *graphics provider is a func of graphics, found in metadata* doesn't mean anything, I need to know what a `__graphicsProvider()` is and what this method (as it appears to be) returns. What is a *Canvas*? There's nothing like that in what looks like a WinForms Project, so there's a chance you have to draw its content to a Bitmap at some point, the `Control.DrawToBitmap()` method may not work with this object (as it doesn't work with some WinForms-native Controls). If I don't know how these things work, I cannot help you. – Jimi Sep 02 '20 at 13:51
  • i believe we can take this post to a chat sometime to solve it better, because too much ideas to share. – Yuza Sep 07 '20 at 09:41

0 Answers0