简体   繁体   中英

How to draw picture in asp.net core controller

I need to draw some simple image in asp.net core controller and return it as jpg/png/bmp/whatever file format. Is it possible in .net core? The image should be easy (ex. square with red color border with given edge size). Are there any libs that would help do that? Thanks!

There are many libraries that allow you to do so, i will recommend you use the regular System.Drawing.Common, here is the link to nuget repo :

https://www.nuget.org/packages/System.Drawing.Common

Code should be as simple as

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);

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