Raster Image Export Example for Blazor

Nevron NOV Chart for Blazor ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All Blazor Chart examples are also available as a stand-alone Visual Studio solution in C#, which you can download from the links below.

Raster Image Export Example for Blazor Description

This example demonstrates how to export images to the clipboard or file selected from an open file dialog. You can also use the SaveToFile and SaveToStream methods of the image exporter class to save to stream or file directly. The format of the image in this case is determined by the file extension.


Raster Image Export Example

C# Raster Image Export Example Code for Nevron Chart for Blazor
Language: C#

using Nevron.Nov;
using Nevron.Nov.Chart;
using Nevron.Nov.Chart.Export;
using Nevron.Nov.Chart.Tools;
using Nevron.Nov.Dom;
using Nevron.Nov.Editors;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NRasterImageExportExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			NChartViewWithCommandBars chartViewWithCommandBars = new NChartViewWithCommandBars();
			m_ChartView = chartViewWithCommandBars.View;
			m_ChartView.Surface.CreatePredefinedChart(ENPredefinedChartType.Cartesian);

			// configure title
			m_ChartView.Surface.Titles[0].Text = "Raster Image Export Example";

			// configure chart
			NCartesianChart chart = (NCartesianChart)m_ChartView.Surface.Charts[0];

			chart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XYZLinear);
            chart.LightModel.SetPredefinedLightModel(ENPredefinedLightModel.BrightCameraLight);
            chart.Projection.SetPredefinedProjection(ENPredefinedProjection.PerspectiveTilted);
            chart.Interactor = new NInteractor(new NTrackballTool());

            // setup X axis
            NLinearScale xScale = (NLinearScale)chart.Axes[ENCartesianAxis.PrimaryX].Scale;

			xScale.MajorGridLines.Visible = true;

			// setup Y axis
			NLinearScale yScale = (NLinearScale)chart.Axes[ENCartesianAxis.PrimaryY].Scale;
			yScale.MajorGridLines.Visible = true;
			
			// add interlaced stripe
			NScaleStrip strip = new NScaleStrip(new NColorFill(NColor.Beige), null, true, 0, 0, 1, 1);
			strip.Interlaced = true;
			yScale.Strips.Add(strip);

			// setup shape series
			NRangeSeries range = new NRangeSeries();
			chart.Series.Add(range);

			range.DataLabelStyle = new NDataLabelStyle(false);
			range.UseXValues = true;
			range.Fill = new NColorFill(NColor.DarkOrange);
			range.Stroke = new NStroke(NColor.DarkRed);

			// fill data
			double[] intervals = new double[] { 5, 5, 5, 5, 5, 5, 5, 5, 5, 15, 30, 60 };
			double[] values = new double[] { 4180, 13687, 18618, 19634, 17981, 7190, 16369, 3212, 4122, 9200, 6461, 3435 };

			int count = Math.Min(intervals.Length, values.Length);
			double x = 0;

			for (int i = 0; i < count; i++)
			{
				double interval = intervals[i];
				double value = values[i];

				double x1 = x;
				double y1 = 0;

				x += interval;
				double x2 = x;
				double y2 = value / interval;

				range.DataPoints.Add(new NRangeDataPoint(x1, y1, x2, y2));
			}

			m_ChartView.Document.StyleSheets.ApplyTheme(new NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series));

			return chartViewWithCommandBars;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			NUniSizeBoxGroup group = new NUniSizeBoxGroup(stack);

            NCheckBox enable3DCheckBox = new NCheckBox("Enable 3D");
            enable3DCheckBox.CheckedChanged += OnEnable3DCheckBoxCheckedChanged;
			enable3DCheckBox.Checked = true;
            stack.Add(enable3DCheckBox);

            NButton copyImageToClipboardButton = new NButton("Copy Image to Clipboard");
			copyImageToClipboardButton.Click += CopyImageToClipboardButton_Click;
			stack.Add(copyImageToClipboardButton);

			NButton saveAsImageFileButton = new NButton("Save as Image File...");
			saveAsImageFileButton.Click += SaveAsImageFileButton_Click;
			stack.Add(saveAsImageFileButton);

			NButton showExportDialogButton = new NButton("Show Export Dialog...");
			showExportDialogButton.Click += ShowExportDialogButton_Click;
			stack.Add(showExportDialogButton);

			return group;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to export images to the clipboard or file selected from an open file dialog. You can also use the SaveToFile and SaveToStream methods of the image exporter class to save to stream or file directly. The format of the image in this case is determined by the file extension.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Chart Raster Image Export", "https://www.nevron.com/docs/openvision/chart-raster-image-export.html"), ("Images Overview", "https://www.nevron.com/docs/openvision/images-overview.html"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnEnable3DCheckBoxCheckedChanged(NValueChangeEventArgs arg) { m_ChartView.Surface.Charts[0].Enable3D = (bool)arg.NewValue; } private void CopyImageToClipboardButton_Click(NEventArgs arg) { NChartRasterImageExporter rasterImageExporter = new NChartRasterImageExporter(m_ChartView.Content); rasterImageExporter.CopyToClipboard(); } private void SaveAsImageFileButton_Click(NEventArgs arg) { NChartRasterImageExporter rasterImageExporter = new NChartRasterImageExporter(m_ChartView.Content); rasterImageExporter.SaveAsImage(); } private void ShowExportDialogButton_Click(NEventArgs arg) { NChartRasterImageExporter rasterImageExporter = new NChartRasterImageExporter(m_ChartView.Content); rasterImageExporter.ShowDialogAsync(m_ChartView.DisplayWindow); } #endregion #region Fields private NChartView m_ChartView; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Raster Image Export Example for Blazor Solution

Related Documentation Links