Print an image

Hi,

I have created a picturebox and drawn some lines in it to build a grid :



Dim bit As New Bitmap(570, 400)
        Dim g As Graphics = Graphics.FromImage(bit)
        Dim i As Integer

        g.DrawLine(New Pen(Color.Black, 2), 5, 5, 5, 395)
        g.DrawLine(New Pen(Color.Black, 2), 5, 395, 565, 395)
        For i = 385 To 5 Step -10
            g.DrawLine(New Pen(Color.Gray), 5, i, 565, i)
        Next
        For i = 375 To 5 Step -20
            g.DrawLine(New Pen(Color.Black, 2), 0, i, 5, i)
        Next
        For i = 22 To 578 Step 17
            g.DrawLine(New Pen(Color.Gray), i, 5, i, 395)
        Next
        For i = 39 To 578 Step 34
            g.DrawLine(New Pen(Color.Black, 2), i, 395, i, 400)
        Next
pbDiagram.Image = bit

 


Now if I try to print the pbDiagram with this code :


Dim g As Image = pbDiagramm.Image
        gr.DrawImage(g, 100, 40)

 


 I get an error message: Image = Null
Why is that and how can I get the printing to work

Thanks


Answer this question

Print an image

  • Alex Duncan

    Hi John,
    thank's for your effort. I have just solved the problem. I was drawing the lines in pbDiagramm in frmMain. Then I created a new Class clsPrint which contains the printing code. In clsPrint I declared a New frmMain to reach my pbDiagramm but by doing so I created a new pbDiagramm wich was empty resulting in the error parameters = null. Idea

    Now I'm doing it like this:
    In frmMain:


    Dim clsPrint As New clsPrint
    clsPrint.graphic = pbDiagramm.Image

     

    in clsPrint:


    Dim graph As Image
    Property graphic() As Image
            Get
                Return graph
            End Get
            Set(ByVal Value As Image)
                graph = Value
            End Set
    End Property

    Sub PrintDiagramm()

            Dim pd As New PrintDocument
            Dim ptdlg As New PrintDialog
            AddHandler pd.PrintPage, AddressOf printpage
            With pd
                .DocumentName = "Diagramm"
            End With
            With ptdlg
                .PrinterSettings = pd.PrinterSettings
                .AllowPrintToFile = False
                .AllowSelection = False
                .AllowSomePages = True
                If .ShowDialog() = DialogResult.OK Then
                    Dim pdlg As New PrintPreviewDialog
                    With pdlg
                        .Document = pd
                        .Width = 700
                        .Height = 700
                        If .ShowDialog() = DialogResult.OK Then
                            pd.Print()
                        End If
                    End With
                Else
                    Return
                End If
            End With
    End Sub

    Private Sub printpage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
    Dim gr As Graphics = e.Graphics
    Dim oImage As Image = graphic
    gr.DrawImage(oImage, 100, 100)
    End Sub

     


    So now it works.And sorry for forgetting to post the complete code in my first post.

    Cheers
    Bruder

  • Docseuss

    Hi Bruder,

    What is gr in gr.DrawImage(g, 100, 40)
    I tested your code and it is good to the line 

    Dim g As Image = pbDiagramm.Image
     

    JohnChen

  • Print an image