Using Aspose.Drawing in ASP.NET Core

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps.

You can use Aspose.Drawing in your ASP.NET Core applications to draw vector graphics, text, and generate images as demonstrated in this tutorial.

1. Create a C# ASP.NET Core project.

In Visual Studio, create a new C# ASP.NET Core Web App (Razor Pages) project.

2. Add the Aspose.Drawing.Common package to WebApplication1 project dependencies.

Aspose.Drawing.Common package

3. Add image drawing code.

Add the Draw.cs file with the following code that draws a gradient and creates an image:

using Aspose.Drawing;
using Aspose.Drawing.Drawing2D;
using Aspose.Drawing.Imaging;
namespace WebApplication1
{
internal class Draw
{
public static void SetLicense()
{
License license = new License();
license.SetLicense("WebApplication1.Aspose.Drawing.NET.lic");
}
public static string DrawToString()
{
return "data:image/png;base64, " + Convert.ToBase64String(DrawToStream(ImageFormat.Png).ToArray());
}
private static MemoryStream DrawToStream(ImageFormat format)
{
Bitmap bitmap = new Bitmap(1000, 800, PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
Brush brush = new LinearGradientBrush(new Point(0, 0), new Point(1000, 800), Color.Red, Color.Blue);
graphics.FillEllipse(brush, 100, 100, 800, 600);
MemoryStream result = new MemoryStream();
bitmap.Save(result, format);
result.Seek(0, SeekOrigin.Begin);
return result;
}
}
}

4. Add an Aspose.Drawing license file.

Copy your Aspose.Drawing.NET.lic file with Aspose.Drawing licensing information to the project directory, open this file properties from Solution Explorer and set Build Action to Embedded resource.

Add the following code to Program.cs - Main:

Draw.SetLicense();

5. Add UI image.

In the Pages\Index.cshtml file, add the following element to the div :

<img src="@Draw.DrawToString()" />

6. Run the application.

Start the project from Visual Studio, a browser will display the gradient image created with Aspose.Drawing:

Linear gradient drawn in ASP.NET Core