简体   繁体   English

ASP.Net Core WebAPI 中 Microsoft Charts 的参考问题

[英]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我正在尝试将 C# .net 框架 MVC API 迁移到 .net Core 3 MVC API 项目,该项目使用System.Windows.Forms.DataVisualisation.Charting.Chart生成 PNG 图表

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.我使用这个 NuGet 包,一切看起来都很好,除了 SaveImage 方法需要对 System.Windows.Forms.Control 类的引用,这不是我的项目的一部分,并且由于该项目是一个 .net Core Web API 项目,这是不可能参考它。

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.错误 CS0012 在未引用的程序集中定义了“控件”类型。 You must add a reference to assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.您必须添加对程序集“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.我的项目是<Project Sdk="Microsoft.NET.Sdk.Web"> ,因此不可能引用Microsoft.WindowsDesktop.App.WindowsForms包。

Is there workaround ?有解决方法吗?

I've found a solution.我找到了解决办法。 Simply by adding the following group into the .csproj file and now it compiles:只需将以下组添加到 .csproj 文件中即可编译:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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