简体   繁体   中英

Reference problem with Microsoft Charts in ASP.Net Core WebAPI

I'm trying to migrate a C# .net framework MVC API to a .net Core 3 MVC API project that generates PNG charts with System.Windows.Forms.DataVisualisation.Charting.Chart

I use this NuGet package and everything looks fine except that the SaveImage method requires a reference to System.Windows.Forms.Control class, which is not part of my project, and as the project is a .net Core Web API project, this is impossible to reference it.

Chart chart = new Chart
{
    BackColor = Color.Transparent,
    Size = new Size(800, 800)
};
ChartArea chartArea = new ChartArea() { BackColor = chart.BackColor };
chart.ChartAreas.Add(chartArea);

// Building of chart here

using (MemoryStream ms = new MemoryStream())
{
    chart.SaveImage(ms, ChartImageFormat.Png); // This method does not compile because it requires System.Windows.Forms.Control
    return ms.ToArray();
}

I have this error:

Error CS0012 The type 'Control' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

My project is <Project Sdk="Microsoft.NET.Sdk.Web"> and therefore it's impossible to reference the Microsoft.WindowsDesktop.App.WindowsForms package.

Is there workaround ?

I've found a solution. Simply by adding the following group into the .csproj file and now it compiles:

  <ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" />
  </ItemGroup>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM