Generate chart image without displaying the WinForms control

Applies to: Nevron Chart for .NET

How to generate chart image without displaying the WinForms control?

Nevron Chart control fully supports windowless image generation (e.g. without being added to a form). For example, you can use the ImageExporter.RenderToBitmap method to generate an image without displaying the WinForms control.



The following code snippets show how to generate a 400x300 bitmap image:

[C#]

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using Nevron.Chart;
using Nevron.Chart.WinForm;
using Nevron.GraphicsCore;
...
using (Bitmap bmp = new Bitmap(400, 300, PixelFormat.Format32bppArgb))
{
    using (NChartControl chartControl = new NChartControl())
    {
        NChart chart = chartControl.Charts[0];
 
        chart.DisplayOnLegend.Visible = false;
        chart.Enable3D = true;
        chart.Width = 100;
        chart.Height = 50;
        chart.Depth = 10;
        chart.Projection.SetPredefinedProjection(PredefinedProjection.Perspective1);
        chart.LightModel.SetPredefinedLightModel(PredefinedLightModel.ShinyCameraLight);
 
        NBarSeries bar = new NBarSeries();
 
        bar.Values.Add(10);
        bar.Values.Add(20);
        bar.Values.Add(30);
 
        chart.Series.Add(bar);
 
        NStyleSheet styleSheet = NStyleSheet.CreatePredefinedStyleSheet(PredefinedStyleSheet.BrightMultiColor);
        styleSheet.Apply(chartControl.Document);
 
        chartControl.ImageExporter.RenderToBitmap(bmp, false);
    }
 
    bmp.Save("c:\\temp\\chart1.bmp");
}

[VB.NET]
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports Nevron.Chart
Imports Nevron.Chart.WinForm
Imports Nevron.GraphicsCore
...
Using bmp As New Bitmap(400, 300, PixelFormat.Format32bppArgb)
    Using chartControl As New NChartControl()
        Dim chart As NChart = chartControl.Charts(0)
 
        chart.DisplayOnLegend.Visible = False
        chart.Enable3D = True
        chart.Width = 100
        chart.Height = 50
        chart.Depth = 10
        chart.Projection.SetPredefinedProjection(PredefinedProjection.Perspective1)
        chart.LightModel.SetPredefinedLightModel(PredefinedLightModel.ShinyCameraLight)
 
        Dim bar As New NBarSeries()
 
        bar.Values.Add(10)
        bar.Values.Add(20)
        bar.Values.Add(30)
 
        chart.Series.Add(bar)
 
        Dim styleSheet As NStyleSheet = NStyleSheet.CreatePredefinedStyleSheet(PredefinedStyleSheet.BrightMultiColor)
        styleSheet.Apply(chartControl.Document)
 
        chartControl.ImageExporter.RenderToBitmap(bmp, False)
    End Using
 
    bmp.Save("c:\temp\chart1.bmp")
End Using

This will create a 400x300 bitmap with a simple bar chart.

You can also consider using the SaveToFile function:

[C#]
using System.Windows.Forms;
using Nevron.Chart;
using Nevron.Chart.WinForm;
using Nevron.GraphicsCore;
...
using (NChartControl chartControl = new NChartControl())
{
    NChart chart = chartControl.Charts[0];
 
    NBarSeries bar = new NBarSeries();
 
    bar.Values.Add(10);
    bar.Values.Add(20);
    bar.Values.Add(30);
 
    chart.Series.Add(bar);
 
    chartControl.ImageExporter.SaveToFile("c:\\temp\\chart.png", new NSize(400, 300), NResolution.ScreenResolution, new NPngImageFormat());
}

[VB.NET]
Imports System.Windows.Forms
Imports Nevron.Chart
Imports Nevron.Chart.WinForm
Imports Nevron.GraphicsCore
...
Using chartControl As New NChartControl()
    Dim chart As NChart = chartControl.Charts(0)
 
    Dim bar As New NBarSeries()
 
    bar.Values.Add(10)
    bar.Values.Add(20)
    bar.Values.Add(30)
 
    chart.Series.Add(bar)
 
    chartControl.ImageExporter.SaveToFile("c:\temp\chart.png", New NSize(400, 300), NResolution.ScreenResolution, New NPngImageFormat())
End Using


Article ID: 211, Created On: 3/19/2012, Modified: 3/19/2012