如何在ASP.NET核心控制器中绘制图片



我需要在ASP.NET Core Controller中绘制一些简单的映像,并将其返回为JPG/PNG/BMP/任何文件格式。在.NET核心中是否有可能?图像应该很容易(例如,带有红色边框的正方形,具有给定的边缘尺寸)。有没有有助于做到这一点的lib?谢谢!

有许多库允许您这样做,我建议您使用常规system.drawing.common,这是Nuget Repo的链接:

https://www.nuget.org/packages/system.common

代码应该像

一样简单
Image image = new Bitmap(2000, 1024);
Graphics graph = Graphics.FromImage(image);
graph.Clear(Color.Azure);
Pen pen = new Pen(Brushes.Black);
graph.DrawLines(pen, new Point[] { new Point(10,10), new Point(800, 900) });
Rectangle rect = new Rectangle(100, 100, 300, 300);
graph.DrawRectangle(pen, rect);    
image.Save("myImage.png", System.Drawing.Imaging.ImageFormat.Png);

最新更新