Working with Image Rendering
Aspose.Drawing for .NET API lets you compose image visual appearance using:
- Alpha Blending
- Antialiasing
- Clipping
Alpha Blending
Alpha blending is the process where the alpha channel is combined with other layers in an image to show translucency. It consists of an additional eight bits that can give value in the range of 0-255 to represent 256 levels of translucency. This blending generates a new color as a result of the alpha channel mixing with the original colors in the image.
To draw graphics with translucency using C#, the following steps can be used.
- Create an object of the Bitmap class
- Initialize the Graphics object from the created bitmap
- Use the Color.FromArgb method with the alpha channel parameter
- Save the output to any desired output image format
// 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"); |
Antialiasing with Lines and Curves
To draw lines and curves with antialiasing in C#, the following steps can be used.
- Create an object of the Bitmap class
- Initialize the Graphics object from the bitmap
- Set the smoothing mode to Antialiasing
- Create the desired line, curve or other object
- Save the output to any desired output image format
// 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"); |
Image Clipping
For image clipping with C#, use the following steps.
- Create an object of the Bitmap class
- Initialize the Graphics object from the bitmap
- Define the clip path using GraphicsPath
- Set the clip path using the SetClip method
// 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"); |