Nevron Forum

Export NChartControl to BitMap or Any Type of Image

https://www.nevron.com/Forum/Topic4170.aspx

By Teddy Lambropoulos - Friday, September 10, 2010

Hi,

Is there a way I can take an NChartControl and export it to a BitMap or any sort of image file?

Thank you,

Teddy

By Nevron Support - Friday, September 10, 2010

Hi Teddy,

Yes the both the WinForm and WebForms controls can be used to dynamically generate an image, without being added to a form. The following code shows how to create a simple bar chart and render it as 400x300 png image:

using Nevron.GraphicsCore;
using Nevron.Chart;
using Nevron.Chart.WinForm;

...

using (NChartControl chartControl = new NChartControl())
{
 // configure chart
 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\\test.png", new NSize(400, 300), NResolution.ScreenResolution, new NPngImageFormat());
}

the control supports JPEG, PNG, BITMAP, TIFF, GIF, Flash, Silverlight and PDF output. You can also save images in memory streams - just use the SaveToStream method of the image exporter.

Hope this helps - questions or comments - please feel free...

 

By Teddy Lambropoulos - Tuesday, January 4, 2011

Ok I figured out how to make a BitMap of my NChartControl. The reason I am having the need to export it to a bitmap is that I have a very large set of data (~150,000 points) in an NGridSurfaceSeries. I need to draw an NLineSeries over this plot however it takes a very long time to dynamically add point 1 of the line series and then point 2 of the line series. My strategy is to convert the NChartControl to a BitMap and then use GDI to draw the line over the NChartControl. Is there a way within an NChartControl I can display a bitmap? So far all I can come up with is this...

Dim NewBitMap_ As New Bitmap(370, 370)

NChart.ImageExporter.RenderToBitmap(NewBitMap, False)

 

What should I do next to accomplish what I stated?

By Nevron Support - Thursday, January 6, 2011

Hi Teddy,

Generally you can display a raster at each chart element that has a fill style - for example:

chart.Wall(ChartWallType.Back).FillStyle = new NImageFillStyle("c:\\temp2\\test.png");

assigns image fill style to the back chart wall. The NImageFillStyle also accepts a constructor taking a bitmap object. Another alternative is to use custom painting:

All Examples\Custom Painting

In your case probably the best solution is to use a feature we have internally, but which is still not exposed/documented - overlay paint callbacks - present in the upcoming SP next week. This will allow you to custom paint any content over the chart without having to refresh. The following code snippet shows a sample use of the OverlayPaintCallback:

  static Bitmap bmp = new Bitmap(100, 100);
  static Point point = new Point();

  class NCustomPaintCallback : NPaintCallback
  {
   public override void OnAfterPaint(NPanel panel, NPanelPaintEventArgs eventArgs)
   {
    eventArgs.Graphics.DeviceGraphics.DrawImage(bmp, point);
   }
  }

  private void Form1_Load(object sender, EventArgs e)
  {
   using (Graphics g = Graphics.FromImage(bmp))
   {
    using (SolidBrush brush = new SolidBrush(Color.Red))
    {
     g.FillRectangle(brush, new RectangleF(0, 0, 100, 100));
    }
   }

   NChart chart = nChartControl1.Charts[0];
   chart.BoundsMode = BoundsMode.Stretch;
   chart.OverlayPaintCallback = new NCustomPaintCallback();

   NBarSeries bar = new NBarSeries();
   bar.Values.Add(10);
   bar.Values.Add(20);
   bar.Values.Add(30);
   bar.FillStyles[0] = new NColorFillStyle(Color.Red);
   bar.FillStyles[1] = new NColorFillStyle(Color.Green);
   bar.FillStyles[2] = new NColorFillStyle(Color.Blue);
   chart.Series.Add(bar);

   nChartControl1.MouseMove += new MouseEventHandler(nChartControl1_MouseMove);
  }

  void nChartControl1_MouseMove(object sender, MouseEventArgs e)
  {
   point = new Point(e.X, e.Y);
   nChartControl1.Invalidate();
  }

Hope this helps - let us know if you meet any problems or have any questions.