Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Chart DrawToBitmap Not Working

Posted on 2012-09-19
Medium Priority
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

Luis Pérez earned 2000 total points
ID: 38416843
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.

Author Closing Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

578 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