Chart DrawToBitmap Not Working

Posted on 2012-09-19
Last Modified: 2012-09-20
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
        .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)
       End With ' ChartSeries

        ' Add the series to the Chart
    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
Question by:SoLost
    LVL 25

    Accepted Solution

    You need to add an area to your chart.

    Just before closing the "with" block (just before the "End With" line), add this code:

    Dim area As System.Windows.Forms.DataVisualization.Charting.ChartArea = New System.Windows.Forms.DataVisualization.Charting.ChartArea
    Dim areaStyle As System.Windows.Forms.DataVisualization.Charting.ChartArea3DStyle = New System.Windows.Forms.DataVisualization.Charting.ChartArea3DStyle(area)

    Open in new window

    Hope that helps.
    LVL 5

    Author Closing Comment

    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! :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now