kirsan31/winforms-datavisualization

how to save chart into image or stream

Closed this issue · 4 comments

Greetings,
I would need to save the graph in an image or in a stream and then place it in a document.
With the Net Framework library it was possible to do it with the "SaveImage" method but trying to do it gives me the following error:

Screenshot_1

is there any way to make it work with this library?

Hi @BigZimos

We defiantly need more info here. This method perfectly working and must have no errors, especially compile time.
Can you at least provide error in text format, that I can translate it? Better yet, the code of the project file, or whole repro project itself.

Hi @kirsan31 sorry and thanks.

The error is:

Type 'Control' is defined in a missing assembly reference. Add a reference to the assembly 'System.Windows.Forms, Version=6.0.2.0, Cultuyre=neutral, PublicKeyToken=b77a5c561934e089'.

This is the code:

using System.Windows.Forms.DataVisualization.Charting;

Chart chart1 = new Chart();
Series s1 = new Series();
chart1.Series.Add(s1);
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pie;
chart1.Series[0].EmptyPointStyle.CustomProperties = "PieLabelStyle=Outside";
chart1.Series[0].IsVisibleInLegend = false;
chart1.Series[0].LabelForeColor = System.Drawing.Color.Transparent;
chart1.Series[0].Name = "s1";
chart1.Series[0].SmartLabelStyle.AllowOutsidePlotArea =
System.Windows.Forms.DataVisualization.Charting.LabelOutsidePlotAreaStyle.Yes;
/Other code for dinamic values/

ChartArea chartArea1 = new ChartArea();
chartArea1.Area3DStyle.Inclination = 55;
chartArea1.Area3DStyle.IsRightAngleAxes = false;
chartArea1.Area3DStyle.Perspective = 25;
chartArea1.Area3DStyle.PointDepth = 190;
chartArea1.InnerPlotPosition.Auto = false;
chartArea1.InnerPlotPosition.Height = 100F;
chartArea1.InnerPlotPosition.Width = 100F;
chartArea1.Name = "ChartArea1";
chart1.ChartAreas.Add(chartArea1);
/Other code for dinamic values/

chart1.Series[0].Points.AddXY(Number1 + "%", Perc);
chart1.Series[0].Points.AddXY(Number2 + "%", Perc);
chart1.Series[0].Points.AddXY(Number3 + "%", Perc);
chart1.Series[0].Points.AddXY(Number4 + "%", Perc);
chart1.Series[0].Points.AddXY(Number5 + "%", Perc);
chart1.Series[0].Points.AddXY(Number6 + "%", Perc);
chart1.Series[0].Points[0].Color = Color1;
chart1.Series[0].Points[1].Color = Color2;
chart1.Series[0].Points[2].Color = Color3;
chart1.Series[0].Points[3].Color = Color4;
chart1.Series[0].Points[4].Color = Color5;
chart1.Series[0].Points[5].Color = Color6;

MemoryStream test = new MemoryStream();
chart1.SaveImage(test, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Png);

Type 'Control' is defined in a missing assembly reference. Add a reference to the assembly 'System.Windows.Forms, Version=6.0.2.0, Cultuyre=neutral, PublicKeyToken=b77a5c561934e089'.

I assumed something like this :) That's why I asked for your project file code (*.csproj). It must be something like this:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>WinExe</OutputType>
		<TargetFramework>net6.0-windows</TargetFramework>
		<UseWindowsForms>true</UseWindowsForms>
	</PropertyGroup>
	
	<ItemGroup>
		<PackageReference Include="WinForms.DataVisualization">
			<Version>1.5.0-pr</Version>
		</PackageReference>
	</ItemGroup>
</Project>

Thank you,
it works now!