Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 21, 2023 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save aspose-com-gists/b8960f80422422251405395636eab772 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/b8960f80422422251405395636eab772 to your computer and use it in GitHub Desktop.
C#.NET examples for 2D drawing engine with System.Drawing compatible API
In .NET, render vector graphics such as lines, curves, figures and text in a variety of fonts, sizes, and styles onto images
using Aspose.Drawing (a .NET based cross-platform 2D drawing engine with System.Drawing compatible API)
// Example to create a new drawing in C# .NET and render as a PNG.
// See https://github.com/aspose-drawing/Aspose.Drawing-for-.NET and https://docs.aspose.com/drawing/net/ for more examples.
// Create a new drawing
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
// Save drawing
bitmap.Save(RunExamples.GetDataDir() + @"drawing.png");
// C# code to draw vector graphics and text, and create image in your Azure Function to run on the cloud.
// See https://docs.aspose.com/drawing/net/using-aspose-drawing-in-azure-function/ for complete details.
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace AzureFunctionApp1
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ExecutionContext context)
{
System.Drawing.AsposeDrawing.License license = new System.Drawing.AsposeDrawing.License();
license.SetLicense(Path.Combine(context.FunctionAppDirectory, "Aspose.Drawing.NET.lic"));
return new FileStreamResult(Draw(ImageFormat.Png), "image/png");
}
static Stream Draw(ImageFormat format)
{
Bitmap bitmap = new Bitmap(1000, 800, PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
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;
}
}
}
// C# code to draw graphics and create image in Blazor WebAssembly App.
// See https://docs.aspose.com/drawing/net/using-aspose-drawing-in-azure-function/ for complete details.
@page "/"
@using System.Drawing;
@using System.Drawing.Drawing2D;
@using System.Drawing.Imaging;
@using System.IO;
<img src="@imageSrc" />
@code {
private string imageSrc;
public Index()
{
imageSrc = "data:image/png;base64, " + Convert.ToBase64String(Draw(ImageFormat.Png).ToArray());
}
static MemoryStream Draw(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;
}
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
System.Drawing.AsposeDrawing.License license = new System.Drawing.AsposeDrawing.License();
license.SetLicense("BlazorApp1.Client.Aspose.Drawing.NET.lic");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Brush brush = new SolidBrush(Color.Blue);
graphics.FillEllipse(brush, 100, 100, 800, 600);
bitmap.Save("Solid.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
static void ConvertFromOneFileFormatToAnother(string srcGraphicsFileFormats, string targetGraphicsFileFormats)
{
new Bitmap(RunExamples.GetDataDir() + @"GraphicsFileFormats\image." + srcGraphicsFileFormats).Save(RunExamples.GetDataDir() + @"GraphicsFileFormats\image_out." + targetGraphicsFileFormats);
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
// Set a transformation that applies to every drawn item:
graphics.RotateTransform(15);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawEllipse(pen, 300, 300, 400, 200);
bitmap.Save("GlobalTransformation.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
GraphicsPath path = new GraphicsPath();
path.AddEllipse(300, 300, 400, 200);
// Set a transformation that applies to the specific path to be drawn:
Matrix matrix = new Matrix();
matrix.RotateAt(45, new Point(500, 400));
path.Transform(matrix);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawPath(pen, path);
bitmap.Save("LocalTransformation.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
Rectangle originalRentangle = new Rectangle(300, 300, 300, 200);
TransformPath(graphics, originalRentangle, (matrix) => matrix.Rotate(15.0f));
TransformPath(graphics, originalRentangle, (matrix) => matrix.Translate(-250, -250));
TransformPath(graphics, originalRentangle, (matrix) => matrix.Scale(0.3f, 0.3f));
bitmap.Save("MatrixTransformations.png");
void TransformPath(Graphics graphics, Rectangle originalRentangle, Action<Matrix> transform)
{
GraphicsPath path = new GraphicsPath();
path.AddRectangle(originalRentangle);
Matrix matrix = new Matrix();
transform(matrix);
path.Transform(matrix);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawPath(pen, path);
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
private static void TransformPath(Graphics graphics, Rectangle originalRentangle, Action<Matrix> transform)
{
GraphicsPath path = new GraphicsPath();
path.AddRectangle(originalRentangle);
Matrix matrix = new Matrix();
transform(matrix);
path.Transform(matrix);
Pen pen = new Pen(Color.FromKnownColor(KnownColor.Blue), 2);
graphics.DrawPath(pen, path);
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
// Set the transformation that maps page coordinates to device coordinates:
graphics.PageUnit = GraphicsUnit.Inch;
Pen pen = new Pen(Color.Blue, 0.1f);
graphics.DrawRectangle(pen, 1, 1, 1, 1);
bitmap.Save("PageTransformation.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
// 1 point is 1/72 inch.
graphics.PageUnit = GraphicsUnit.Point;
graphics.DrawRectangle(new Pen(Color.Red, 36f), 72, 72, 72, 72);
// 1 mm is 1/25.4 inch.
graphics.PageUnit = GraphicsUnit.Millimeter;
graphics.DrawRectangle(new Pen(Color.Green, 6.35f), 25.4f, 25.4f, 25.4f, 25.4f);
graphics.PageUnit = GraphicsUnit.Inch;
graphics.DrawRectangle(new Pen(Color.Blue, 0.125f), 1, 1, 1, 1);
bitmap.Save("UnitsOfMeasure.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
// Set the transformation that maps world coordinates to page coordinates:
graphics.TranslateTransform(500, 400);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawRectangle(pen, 0, 0, 300, 200);
bitmap.Save("WorldTransformation.png");
// C# code to draw an ellipse and save it as an image in a Docker container.
// See https://docs.aspose.com/drawing/net/how-to-run-aspose-drawing-in-docker/ for complete details.
using System.Drawing;
using System.Drawing.Drawing2D;
System.Drawing.AsposeDrawing.License license = new System.Drawing.AsposeDrawing.License();
// TODO: Use your license file
license.SetLicense("Aspose.Drawing.NET.lic");
Bitmap bitmap = new Bitmap(1000, 800);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.SmoothingMode = SmoothingMode.HighQuality;
Brush brush = new LinearGradientBrush(new Point(0, 0), new Point(1000, 800), Color.Red, Color.Blue);
graphics.FillEllipse(brush, 100, 100, 800, 600);
bitmap.Save(Path.Combine("TestOut", "gradient.png"));
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
// Create a new drawing
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
// Save drawing
bitmap.Save("drawing.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
Bitmap image = new Bitmap(RunExamples.GetDataDir() + @"Images\aspose_logo.png");
// Select the top left part of the image with the logo:
Rectangle sourceRectangle = new Rectangle(0, 0, 50, 40);
Rectangle destinationRectangle = sourceRectangle;
graphics.DrawImage(image, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel);
bitmap.Save(RunExamples.GetDataDir() + @"Images\Cropping_out.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
Bitmap sourceBitmap = new Bitmap(RunExamples.GetDataDir() + @"Images\aspose_logo.png");
Bitmap targetBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Directly copy pixel data from the source to the target bitmap:
int[] pixels = new int[sourceBitmap.Width * sourceBitmap.Height];
sourceBitmap.ReadArgb32Pixels(pixels);
targetBitmap.WriteArgb32Pixels(pixels);
targetBitmap.Save(RunExamples.GetDataDir() + @"Images\DirectDataAccess_out.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
// Load an image:
Bitmap image = new Bitmap(RunExamples.GetDataDir() + @"Images\aspose_logo.png");
// Draw the image:
graphics.DrawImage(image, 0, 0);
bitmap.Save(RunExamples.GetDataDir() + @"Images\Display_out.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
// Load the image:
Bitmap bitmap = new Bitmap(RunExamples.GetDataDir() + @"Images\aspose_logo.png");
// Save the image:
bitmap.Save(RunExamples.GetDataDir() + @"Images\LoadSave_out.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
Bitmap image = new Bitmap(RunExamples.GetDataDir() + @"Images\aspose_logo.png");
// Scale the image 5x:
Rectangle expansionRectangle = new Rectangle(0, 0, image.Width * 5, image.Height * 5);
graphics.DrawImage(image, expansionRectangle);
bitmap.Save(RunExamples.GetDataDir() + @"Images\Scale_out.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
// Initialize license object
System.Drawing.AsposeDrawing.License license = new System.Drawing.AsposeDrawing.License(); // For Aspose.Drawing package.
// Aspose.Drawing.License license = new Aspose.Drawing.License(); // For Aspose.Drawing.Common package.
// Set license
license.SetLicense("Aspose.Drawing.lic");
System.Console.WriteLine("License set successfully.");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System;
using System.IO;
// Initialize license object
System.Drawing.AsposeDrawing.License license = new System.Drawing.AsposeDrawing.License(); // For Aspose.Drawing package.
// Aspose.Drawing.License license = new Aspose.Drawing.License(); // For Aspose.Drawing.Common package.
// Load license in FileStream
FileStream myStream = new FileStream("Aspose.Drawing.lic", FileMode.Open);
// Set license
license.SetLicense(myStream);
Console.WriteLine("License set successfully.");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
// Initialize metered object
System.Drawing.AsposeDrawing.Metered metered = new System.Drawing.AsposeDrawing.Metered(); // For Aspose.Drawing package.
// Aspose.Drawing.Metered metered = new Aspose.Drawing.Metered(); // For Aspose.Drawing.Common package.
// Set metered public and private keys
metered.SetMeteredKey("*****", "*****");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawArc(pen, 0, 0, 700, 700, 0, 180);
bitmap.Save("DrawArc.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
PointF p1 = new PointF(0, 0); // start point
PointF c1 = new PointF(0, 800); // first control point
PointF c2 = new PointF(1000, 0); // second control point
PointF p2 = new PointF(1000, 800); // end point
graphics.DrawBezier(pen, p1, c1, c2, p2);
bitmap.Save("DrawBezierSpline.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawCurve(pen, new Point[] { new Point(10, 700), new Point(250, 500), new Point(500, 10), new Point(750, 500), new Point(990, 700) });
bitmap.Save("DrawCardinalSpline.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawClosedCurve(pen, new Point[] { new Point(100, 700), new Point(350, 600), new Point(500, 500), new Point(650, 600), new Point(900, 700) });
bitmap.Save("DrawClosedCurve.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawEllipse(pen, 10, 10, 900, 700);
bitmap.Save("DrawEllipse.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawLine(pen, 10, 700, 500, 10);
graphics.DrawLine(pen, 500, 10, 990, 700);
bitmap.Save("DrawLines.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
GraphicsPath path = new GraphicsPath();
path.AddLine(100, 100, 1000, 400);
path.AddLine(1000, 600, 300, 600);
path.AddRectangle(new Rectangle(500, 350, 200, 400));
path.AddEllipse(10, 250, 450, 300);
graphics.DrawPath(pen, path);
bitmap.Save("DrawPath.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawPolygon(pen, new Point[] { new Point(100, 100), new Point(500, 700), new Point(900, 100) });
bitmap.Save("DrawPolygon.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawRectangle(pen, 10, 10, 900, 700);
bitmap.Save("DrawRectangle.png");
using (var image = Image.FromFile(Path.Combine(RunExamples.GetDataDir(), "UseCases", "cat.jpg")))
{
var graphics = Graphics.FromImage(image);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
graphics.PageUnit = GraphicsUnit.Pixel;
var pen = new Pen(Color.Magenta, 1);
int gap = 2;
graphics.DrawRectangle(pen, 0, 0, image.Width - 1, image.Height - 1);
graphics.DrawRectangle(pen, gap, gap, image.Width - gap - 1, image.Height - gap - 1);
image.Save(Path.Combine(RunExamples.GetDataDir(), "UseCases", "cat_with_honor.jpg"));
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
GraphicsPath path = new GraphicsPath();
path.AddPolygon(new Point[] { new Point(100, 400), new Point(500, 100), new Point(900, 400), new Point(500, 700) });
Region region = new Region(path);
GraphicsPath innerPath = new GraphicsPath();
innerPath.AddRectangle(new Rectangle(300, 300, 400, 200));
region.Exclude(innerPath);
Brush brush = new SolidBrush(Color.Blue);
graphics.FillRegion(brush, region);
bitmap.Save("FillRegion.png");
// C# code to draw a gradient and save image in your .NET 6 application.
// See https://docs.aspose.com/drawing/net/using-aspose-drawing-in-net6/ for complete details.
using System.Drawing;
using System.Drawing.Drawing2D;
System.Drawing.AsposeDrawing.License license = new System.Drawing.AsposeDrawing.License();
license.SetLicense("Aspose.Drawing.NET.lic");
Bitmap bitmap = new Bitmap(1000, 800);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.SmoothingMode = SmoothingMode.HighQuality;
Brush brush = new LinearGradientBrush(new Point(0, 0), new Point(1000, 800), Color.Red, Color.Blue);
graphics.FillEllipse(brush, 100, 100, 800, 600);
bitmap.Save("gradient.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen bluePen = new Pen(Color.Blue, 2);
graphics.DrawLine(bluePen, 100, 100, 900, 100);
Pen redPen = new Pen(Color.FromArgb(255, 255, 0, 0), 2);
graphics.DrawLine(redPen, 100, 200, 900, 200);
bitmap.Save("Colors.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
DrawPath(graphics, LineJoin.Bevel, 200);
DrawPath(graphics, LineJoin.Round, 400);
bitmap.Save("Join.png");
void DrawPath(Graphics graphics, LineJoin join, int y)
{
Pen pen = new Pen(Color.Blue, 30);
GraphicsPath path = new GraphicsPath();
path.StartFigure();
path.AddLine(100, y, 200, y);
path.AddLine(200, y, 200, y + 100);
pen.LineJoin = join;
graphics.DrawPath(pen, path);
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
private static void DrawPath(Graphics graphics, LineJoin join, int y)
{
Pen pen = new Pen(Color.FromKnownColor(KnownColor.Blue), 30);
GraphicsPath path = new GraphicsPath();
path.StartFigure();
path.AddLine(100, y, 200, y);
path.AddLine(200, y, 200, y + 100);
pen.LineJoin = join;
graphics.DrawPath(pen, path);
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
for (int i = 1; i < 8; ++i)
{
Pen pen = new Pen(Color.Blue, i);
graphics.DrawLine(pen, 100, i * 100, 900, i * 100);
}
bitmap.Save("Width.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.FillEllipse(new SolidBrush(Color.FromArgb(128, 255, 0, 0)), 300, 100, 400, 400);
graphics.FillEllipse(new SolidBrush(Color.FromArgb(128, 0, 255, 0)), 200, 300, 400, 400);
graphics.FillEllipse(new SolidBrush(Color.FromArgb(128, 0, 0, 255)), 400, 300, 400, 400);
bitmap.Save("AlphaBlending.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.Clear(Color.White);
Pen pen = new Pen(Color.Black, 1);
graphics.DrawEllipse(pen, 10, 10, 980, 780);
graphics.DrawCurve(pen, new Point[] { new Point(10, 700), new Point(250, 500), new Point(500, 10), new Point(750, 500), new Point(990, 700) });
graphics.DrawLine(pen, 20, 20, 980, 780);
bitmap.Save("Antialiasing.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
Rectangle rectangle = new Rectangle(200, 200, 600, 400);
GraphicsPath clipPath = new GraphicsPath();
clipPath.AddEllipse(rectangle);
graphics.SetClip(clipPath);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
Brush brush = new SolidBrush(Color.White);
Font arial = new Font("Arial", 20, FontStyle.Regular);
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus.";
graphics.DrawString(text, arial, brush, rectangle, stringFormat);
bitmap.Save("Clipping.png");
// callout with size
using (var image = Image.FromFile(Path.Combine(RunExamples.GetDataDir(), "UseCases", "gears.png")))
{
var graphics = Graphics.FromImage(image);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
graphics.PageUnit = GraphicsUnit.Pixel;
DrawCallOut(graphics, new PointF(107, 55), new PointF(179, 5), 74, "mm");
DrawCallOut(graphics, new PointF(111, 146), new PointF(29, 180), 28, "mm");
image.Save(Path.Combine(RunExamples.GetDataDir(), "UseCases", "gears_with_callout.jpg"));
}
void DrawCallOut(Graphics graphic, PointF startAnchor, PointF endAnchor, int value, string unit)
{
Pen pen = new Pen(Color.LightGray, 1);
Font font = new Font("Arial", 10, FontStyle.Bold);
string outputValue = $"{value} {unit}";
var textSize = graphic.MeasureString(outputValue, font);
int diameterSymbolSize = 12;
int spaceSize = 3;
textSize.Width += diameterSymbolSize + spaceSize;
float callOutMiddleX = endAnchor.X > startAnchor.X ? endAnchor.X - textSize.Width : endAnchor.X + textSize.Width;
float callOutMiddleY = endAnchor.Y > startAnchor.Y ? endAnchor.Y - textSize.Height : endAnchor.Y + textSize.Height;
graphic.DrawLine(pen, startAnchor.X, startAnchor.Y, callOutMiddleX, callOutMiddleY);
float textAnchorX = Math.Min(callOutMiddleX, endAnchor.X);
float textAnchorY = callOutMiddleY;
graphic.DrawLine(pen, callOutMiddleX, callOutMiddleY,
textAnchorX == callOutMiddleX ? textAnchorX + textSize.Width : textAnchorX,
callOutMiddleY);
graphic.DrawEllipse(pen, new Rectangle((int)textAnchorX + spaceSize,
(int)(textAnchorY - textSize.Height) + spaceSize, 10, 10));
graphic.DrawLine(pen, (int)textAnchorX + 1, (int)textAnchorY - 1,
(int)textAnchorX + diameterSymbolSize + 2,
(int)textAnchorY - diameterSymbolSize - 2);
SolidBrush brush = new SolidBrush(Color.LightGray);
graphic.DrawString(outputValue, font, brush, (int)textAnchorX + diameterSymbolSize + spaceSize,
(int)(textAnchorY - textSize.Height));
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Text;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
graphics.Clear(Color.White);
Brush brush = new SolidBrush(Color.Black);
Pen pen = new Pen(Color.Blue, 1);
Font arial = new Font("Arial", 20, FontStyle.Regular);
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus.";
Rectangle rectangle = new Rectangle(100, 100, 800, 600);
graphics.DrawRectangle(pen, rectangle);
graphics.DrawString(text, arial, brush, rectangle);
bitmap.Save("DrawText.png");
using (var image = Image.FromFile(Path.Combine(RunExamples.GetDataDir(), "UseCases", "girl.jpg")))
{
var graphics = Graphics.FromImage(image);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
graphics.PageUnit = GraphicsUnit.Pixel;
SolidBrush brush = new SolidBrush(Color.Navy);
Font font = new Font("Calibri", 20, FontStyle.Italic);
int padding = 5;
string text = "Happy Birthday!";
var words = text.Split(' ');
int extentWidth = 0;
int extentHeight = 0;
words.ToList().ForEach(word => { var stringSize = graphics.MeasureString(word, font);
extentWidth = Math.Max(extentWidth, (int)stringSize.Width + padding);
extentHeight += (int)stringSize.Height; });
Rectangle rectangle = new Rectangle(image.Width - padding - extentWidth,
image.Height - padding - extentHeight,
extentWidth,
extentHeight);
graphics.DrawString(text, font, brush, rectangle);
image.Save(Path.Combine(RunExamples.GetDataDir(), "UseCases", "girl_card.jpg"));
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Text;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
graphics.Clear(Color.White);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
Brush brush = new SolidBrush(Color.Black);
Pen pen = new Pen(Color.Blue, 1);
Font arial = new Font("Arial", 20, FontStyle.Regular);
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus.";
Rectangle rectangle = new Rectangle(100, 100, 800, 600);
graphics.DrawRectangle(pen, rectangle);
graphics.DrawString(text, arial, brush, rectangle, stringFormat);
bitmap.Save("FormatText.png");
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Text;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
graphics.Clear(Color.White);
DrawText(graphics, "Arial", 100);
DrawText(graphics, "Times New Roman", 200);
DrawText(graphics, "Verdana", 300);
bitmap.Save("Hinting.png");
void DrawText(Graphics graphics, string familyName, int y)
{
Brush brush = new SolidBrush(Color.Black);
Font font = new Font(familyName, 10, FontStyle.Regular);
string text = "The quick brown fox jumps over the lazy dog. 0123456789 ~!@#$%^&*()_+-={}[];':\"<>?/,.\\¹`";
graphics.DrawString(text, font, brush, 100, y);
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
private static void DrawText(Graphics graphics, string familyName, int y)
{
Brush brush = new SolidBrush(Color.FromKnownColor(KnownColor.Black));
Font font = new Font(familyName, 10, FontStyle.Regular);
string text = "The quick brown fox jumps over the lazy dog. 0123456789 ~!@#$%^&*()_+-={}[];':\"<>?/,.\\№`";
graphics.DrawString(text, font, brush, 100, y);
}
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Text;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
graphics.Clear(Color.White);
Brush brush = new SolidBrush(Color.Black);
InstalledFontCollection fonts = new InstalledFontCollection();
Font arial = new Font("Arial", 20, FontStyle.Regular);
graphics.DrawString(fonts.Families.Length + " installed font families.", arial, brush, 100, 100);
for (int i = 0; i < 6 && i < fonts.Families.Length; ++i)
{
graphics.DrawString(fonts.Families[i].Name, arial, brush, 100, (i + 2) * 100);
}
bitmap.Save("InstalledFonts.png");
@BillieJo1
Copy link

Most impressive got to hand it to ya competing in the t to just pester one other a stranger in the dark keep it going

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment