Link to home
Start Free TrialLog in
Avatar of SoLost
SoLostFlag for New Zealand

asked on

Chart DrawToBitmap Not Working

Hi there,
I am trying to generate some charts and render them to an image.  If I put an chart on the form, populate it with data and then call Chart.DrawToBitmap it works fine.  However, if I create the Chart as an object in my code then it just renders an empty image.

Dim Chart1 As New System.Windows.Forms.DataVisualization.Charting.Chart
    With Chart1
        .Legends.Clear()
        .Width = 100
        .Height = 100

        Dim ChartSeries As New System.Windows.Forms.DataVisualization.Charting.Series
        With ChartSeries
            .Name = "TestSeries"
            .ChartType = DataVisualization.Charting.SeriesChartType.Line
            
           ' Add sample data
            For i As Integer = 1 To 10
                .Points.AddXY(i, i * 2)
            Next
       End With ' ChartSeries

        ' Add the series to the Chart
        .Series.Clear()
        .Series.Add(ChartSeries)
    End With ' Chart1

    Dim img As New Bitmap(100, 100)
    Chart1.DrawToBitmap(bmp, New Rectangle(0, 0, 100, 100))

    bmp.Save("C:\Test.jpg", ImageFormat.Jpeg)

Open in new window


The above code just renders a blank image.

But as I said, if I drag a Chart object from the toolbox and put it on the form and populate the series, it renders to an image just fine.

There must be something that I'm neglecting to do.

Please help
ASKER CERTIFIED SOLUTION
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

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
Avatar of SoLost

ASKER

Fantastic!!!  That worked!

I figured that there was something that was happening when it rendered to the form but I couldn't find any examples anywhere!

Thanks again! :)