Here's C# code that creates a 100x50 rectangle from polygon points and renders it to a BMP file:
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Program
{
static void Main()
{
int canvasWidth = 300;
int canvasHeight = 200;
using (Bitmap bmp = new Bitmap(canvasWidth, canvasHeight))
using (Graphics g = Graphics.FromImage(bmp))
{
// Clear background to white
g.Clear(Color.White);
// Define rectangle as polygon points (100x50 rectangle)
// Starting at position (50, 75) to center it roughly on canvas
Point[] rectanglePoints = {
new Point(50, 75), // Top-left
new Point(150, 75), // Top-right (50 + 100)
new Point(150, 125), // Bottom-right (75 + 50)
new Point(50, 125) // Bottom-left
};
// Draw the rectangle outline
using (Pen blackPen = new Pen(Color.Black, 2))
{
g.DrawPolygon(blackPen, rectanglePoints);
}
// Optional: Fill the rectangle
using (Brush fillBrush = new SolidBrush(Color.LightBlue))
{
g.FillPolygon(fillBrush, rectanglePoints);
}
// Save as BMP
bmp.Save("rectangle.bmp", ImageFormat.Bmp);
}
Console.WriteLine("Rectangle BMP created: rectangle.bmp");
Console.WriteLine("Rectangle dimensions: 100x50 pixels");
}
}
This code creates a 100×50 pixel rectangle by defining it as a polygon with four corner points:
- (50, 75) - top-left corner
- (150, 75) - top-right corner (50 + 100 width)
- (150, 125) - bottom-right corner (75 + 50 height)
- (50, 125) - bottom-left corner
The rectangle is drawn using DrawPolygon() for the outline and FillPolygon() for the fill, treating it as vector geometry rather than using the built-in DrawRectangle() method.
The resulting BMP file will contain a light blue filled rectangle with a black border, positioned roughly in the center of a 300×200 canvas.