Export NChartControl to BitMap or Any Type of Image


Author
Message
Teddy Lambropoulos
Teddy Lambropoulos
Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)
Group: Forum Members
Posts: 20, Visits: 1

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


Nevron Support
Nevron Support
Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)
Group: Administrators
Posts: 3.1K, Visits: 4.2K

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...

 



Best Regards,
Nevron Support Team


Teddy Lambropoulos
Teddy Lambropoulos
Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)Junior Member (20 reputation)
Group: Forum Members
Posts: 20, Visits: 1

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?


Nevron Support
Nevron Support
Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)Supreme Being (4.5K reputation)
Group: Administrators
Posts: 3.1K, Visits: 4.2K

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.



Best Regards,
Nevron Support Team


GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search