How to draw a star with basic graphics commands in C#

How to draw a star with basic graphics commands in C#

Drawing a star in C# is the process of creating a star-shaped geometric figure using graphics APIs, drawing libraries, or low-level drawing commands. A star can be rendered using connected line segments, polygon paths, vector graphics instructions, or custom mathematical calculations.

Star shapes are commonly used in dashboards, rating systems, games, educational software, diagramming tools, chart annotations, and custom user interface components.

  • Create rating and review controls.
  • Draw custom vector graphics.
  • Generate icons dynamically.
  • Build game assets and effects.
  • Render scalable UI elements.

When and why we draw stars?

Many applications require geometric shapes that are not directly available as built-in controls. A star is one of the most frequently requested custom shapes because it is visually distinctive and easy for users to recognize.

Instead of storing multiple image files, developers can generate stars programmatically, allowing the shape to scale to any size without losing quality.

  • Resolution-independent rendering.
  • Dynamic sizing and coloring.
  • Reduced image asset management.
  • Improved customization capabilities.
  • Better support for themes and dark mode.

Are There Libraries to Draw Stars in C#?

Yes. Several graphics libraries can be used to draw star shapes in C#. Most developers use the built-in System.Drawing namespace for Windows Forms applications, while modern applications may use alternative graphics frameworks.

Library Platform Star Drawing Support
System.Drawing WinForms Custom polygon implementation required
SkiaSharp Cross Platform Path-based drawing
Avalonia Desktop Geometry and Path support
WPF Windows PathGeometry support
ImageSharp.Drawing Cross Platform Polygon drawing support

For most C# desktop applications, System.Drawing provides everything necessary to create custom star shapes using lines and paths.

How Can I Draw a Star with a Graphics Library?

The easiest approach is generating star vertices mathematically and drawing the resulting polygon using Graphics.FillPolygon or Graphics.DrawPolygon.

using System;
using System.Drawing;

public static PointF[] CreateStarPoints( float centerX, float centerY,
        float outerRadius, float innerRadius, int points)
{
    PointF[] vertices = new PointF[points * 2];
    double angle = -Math.PI / 2;
    double step = Math.PI / points;

    for (int i = 0; i < points * 2; i++)
    {
        float radius = (i % 2 == 0) ? outerRadius : innerRadius;
        vertices[i] = new PointF( centerX + radius * (float)Math.Cos(angle), centerY + radius * (float)Math.Sin(angle));
        angle += step;
    }

    return vertices;
}

Drawing the generated star:

PointF[] star = CreateStarPoints( 150, 150, 100, 40, 5);

graphics.FillPolygon(Brushes.Gold, star);
graphics.DrawPolygon(Pens.Black, star);

How Can I Draw a Star with Basic Graphics Commands?

In some projects, developers prefer not to use helper methods or polygon abstractions. Instead, they create the star directly with line and path commands.

using System.Drawing.Drawing2D;

GraphicsPath path = new GraphicsPath();

PointF[] star = {
    new PointF(100, 10), new PointF(122, 75),
    new PointF(190, 75), new PointF(135, 115),
    new PointF(155, 180), new PointF(100, 140),
    new PointF(45, 180), new PointF(65, 115),
    new PointF(10, 75), new PointF(78, 75)
};

path.AddLines(star);
path.CloseFigure();

graphics.FillPath(Brushes.Gold, path);
graphics.DrawPath(Pens.Black, path);

This method relies entirely on path and line commands. The same approach can be used in other graphics frameworks because nearly all vector APIs provide line and path primitives.

An Alternative Star Implementation: Rounded Corners Star

A traditional star contains sharp corners. Modern user interfaces often use rounded corners to create a softer and more polished appearance.

One approach is replacing corner intersections with Bezier curves.

using System.Drawing.Drawing2D;

GraphicsPath path = new GraphicsPath();

path.AddBezier( new PointF(100, 20), new PointF(105, 10), new PointF(115, 10), new PointF(120, 20));
path.AddLine(120, 20, 145, 75);
path.AddBezier( new PointF(145, 75), new PointF(150, 82), new PointF(150, 88), new PointF(145, 95));
path.AddLine(145, 95, 100, 130);

path.CloseFigure();

graphics.FillPath(Brushes.Gold, path);
graphics.DrawPath(Pens.DarkGoldenrod, path);

In a production implementation, Bezier curves would be generated automatically around every star vertex. This technique creates a modern icon-style appearance while preserving the overall star shape.

How to Use This Method Correctly?

Star generation algorithms are simple, but several details can significantly affect rendering quality and performance.

  • Always calculate points relative to a center position.
  • Keep inner and outer radius values proportional.
  • Use anti-aliasing for smoother edges.
  • Reuse GraphicsPath instances when possible.
  • Dispose drawing resources properly.
  • Scale coordinates instead of storing multiple versions.
  • Test rendering at different DPI settings.

For high-quality rendering, enable anti-aliasing before drawing:

graphics.SmoothingMode = SmoothingMode.AntiAlias;

Star Drawing Performance Considerations

Drawing a single star is inexpensive, but applications that render hundreds or thousands of shapes should avoid unnecessary allocations.

Recommendation Benefit
Reuse point arrays Less memory allocation
Cache GraphicsPath objects Faster rendering
Enable double buffering Reduced flickering
Use anti-aliasing selectively Better performance balance

Conclusion

Drawing a star in C# can be achieved using simple line commands, GraphicsPath operations, polygon rendering methods, or custom mathematical algorithms. The best approach depends on the level of control required by the application.

For most projects, generating star vertices mathematically and rendering them through Graphics.DrawPolygon or GraphicsPath provides an excellent combination of flexibility, performance, and visual quality.

Contents related to 'How to draw a star with basic graphics commands in C#'

How to draw an arrow with DrawLine command
How to draw an arrow with DrawLine command
How to draw a diamond with basic graphics commands in C#
How to draw a diamond with basic graphics commands in C#