Are you blogging on PH? Get your free blog.

VB.NET

Moderators: seancampbell
Number of threads: 3618
Number of posts: 9200

This Forum Only
Post New Thread

Report
Printing picturebox on printer Posted by marius40 on 10 Nov 2009 at 12:30 PM
I would like to print the content of a picturebox to the printer.
I am using the code below. I am sending the graph to the printer, the same way i send it to the picturebox. BUT, only the grid and text gets printed. The bars of the graph is not printed.

What am i doing wrong

Private Sub btnPrintGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintGraph.Click
        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim canvas As Graphics = e.Graphics
        Dim heighestamount As Integer
        ' ----- Create an array of data points to plot.
        Dim chartData() As Single = {mark0, mark10, mark20, mark30, mark40, mark50, mark60, mark70, mark80, mark90}
        ' ----- Create some pens.
        heighestamount = 0
        For a = 0 To 9
            If heighestamount < chartData(a) Then
                heighestamount = chartData(a)
            End If
        Next
        Dim penRed As New Pen(Color.Red, -1)
        Dim penBlack As New Pen(Color.Black, -1)
        Dim penShadow As New Pen(Color.Gray, -1)
        ' ----- Prepare to add labels.
        Dim labelFont As New Font("Arial", 3, FontStyle.Regular)
        Dim labelFont2 As New Font("Arial", 5, FontStyle.Regular)
        Dim labelBrush As New SolidBrush(Color.Blue)
        ' ----- Used to plot the various elements.
        Dim x1, y1 As Single 'Lower left corner
        Dim x2, y2 As Single 'Upper right corner
        Dim scaleX, scaleY As Single
        Dim xScan, yScan As Single
        Dim oneBar As RectangleF
        ' ----- Set the scaling.
        x1 = -20
        y1 = -20
        x2 = 110
        y2 = 110
        scaleX = PictureBox1.ClientSize.Width / (x2 - x1)
        scaleY = PictureBox1.ClientSize.Height / (y2 - y1)
        canvas.ScaleTransform(scaleX, -scaleY) '(inverted)
        canvas.TranslateTransform(-x1, -y2) '(inverted)
        ' ----- Color the background.
        canvas.Clear(Color.Cornsilk)

        ' ----- Draw chart outline rectangle.
        canvas.DrawRectangle(penBlack, New Rectangle(0, 0, 100, 100))

        ' ----- Draw the chart grid.
        For xScan = 10 To 90 Step 10
            canvas.DrawLine(penBlack, xScan, 0, xScan, 100)
        Next xScan
        For yScan = 1 To 100 Step 100 / heighestamount
            canvas.DrawLine(penBlack, 0, yScan, 100, yScan)
        Next yScan

        ' ----- Draw some shadowed bars.
        For xScan = 0 To 90 Step 10
            ' ----- Draw the shadow first.
            oneBar.X = xScan + 0.6
            oneBar.Y = 0
            oneBar.Width = 6
            oneBar.Height = (chartData((xScan \ 10)) * 5 - 2)
            canvas.FillRectangle(New SolidBrush(Color.FromArgb(127, Color.Gray)), oneBar)

            ' ----- Now draw the bars in front.
            oneBar.X = xScan + 2
            oneBar.Y = 0
            oneBar.Height = (chartData((xScan \ 10)) * 5)
            canvas.FillRectangle(New SolidBrush(Color.Red), oneBar)
        Next xScan
        ' ----- Need to un-invert the scaling so text labels are
        '       right-side-up.
        canvas.ResetTransform()
        canvas.ScaleTransform(scaleX, scaleY)
        canvas.TranslateTransform(-x1, -y1)
        ' ----- Label the Y-axis.
        For yScan = 1 To 100 Step 100 / heighestamount
            canvas.DrawString(Math.Round(yScan / heighestamount, 0).ToString, labelFont, labelBrush, _
               -2 * yScan.ToString.Length - 3, 98 - (yScan))
        Next yScan
        ' ----- Label the X-axis.
        For xScan = 0 To 100 Step 10
            canvas.DrawString(xScan.ToString, labelFont, labelBrush, _
               xScan + 1.7 - 2 * xScan.ToString.Length, 93)
        Next xScan
        canvas.DrawString("Distrubution Chart:" & grouping & " " & testname, labelFont2, Brushes.Red, 5, 100)
        ' ----- Clean up.
        labelFont.Dispose()
        labelBrush.Dispose()
        penRed.Dispose()
        penBlack.Dispose()
        penShadow.Dispose()
        canvas = Nothing
        mark0 = 0
        mark10 = 0
        mark20 = 0
        mark30 = 0
        mark40 = 0
        mark50 = 0
        mark60 = 0
        mark70 = 0
        mark80 = 0
        mark90 = 0
    End Sub




 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.