Certainly! Below is a simple example in C# that renders vector geometry (lines, rectangles, ellipses, polygons, etc.) to a .bmp file using the System.Drawing namespace. This code will create a bitmap, draw some vector shapes, and save it as a BMP file.
Note:
This code works on Windows and requires the System.Drawing.Common NuGet package if you are using .NET Core or .NET 5/6/7+.
For cross-platform vector-to-bitmap rendering, consider using SkiaSharp.
Example: Render Vector Geometry to BMP
csharp
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
class Program
{
static void Main()
{
int width = 400;
int height = 300;
using (Bitmap bmp = new Bitmap(width, height))
using (Graphics g = Graphics.FromImage(bmp))
{
// White background
g.Clear(Color.White);
// Draw a red line
using (Pen redPen = new Pen(Color.Red, 3))
{
g.DrawLine(redPen, 10, 10, 390, 290);
}
// Draw a green rectangle
using (Pen greenPen = new Pen(Color.Green, 2))
{
g.DrawRectangle(greenPen, 50, 50, 100, 80);
}
// Draw a blue ellipse
using (Pen bluePen = new Pen(Color.Blue, 2))
{
g.DrawEllipse(bluePen, 200, 100, 150, 100);
}
// Draw a filled polygon
Point[] polygonPoints = {
new Point(300, 50),
new Point(350, 80),
new Point(320, 130),
new Point(270, 120)
};
using (Brush brush = new SolidBrush(Color.FromArgb(128, 255, 0, 0)))
{
g.FillPolygon(brush, polygonPoints);
}
// Save as BMP
bmp.Save("output.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
Console.WriteLine("BMP file created: output.bmp");
}
}
How it works
Bitmap: Creates a blank bitmap.
Graphics: Used to draw vector shapes onto the bitmap.
DrawLine, DrawRectangle, DrawEllipse, FillPolygon: Draws various vector shapes.
Save: Exports the bitmap as a .bmp file.
To run this code
Create a new C# console project.
Add a reference to System.Drawing.Common (if using .NET Core/5/6/7+):
code
dotnet add package System.Drawing.Common
Paste the code into Program.cs.
Run the project.
The file output.bmp will be created in your project directory.
If you want to render more complex vector geometry (SVG, paths, etc.), let me know!