How to Generate Image using C#
using System;
using System.Drawing;
class Program
{
static void Main()
{
// Create a new Bitmap object with desired width and height
int width = 400;
int height = 300;
Bitmap image = new Bitmap(width, height);
// Get a Graphics object from the image
using (Graphics graphics = Graphics.FromImage(image))
{
// Draw on the image using the Graphics object
graphics.Clear(Color.White); // Set the background color
// Draw a rectangle
Pen pen = new Pen(Color.Red, 2);
Rectangle rect = new Rectangle(50, 50, 300, 200);
graphics.DrawRectangle(pen, rect);
// Draw some text
Font font = new Font("Arial", 14);
Brush brush = new SolidBrush(Color.Blue);
graphics.DrawString("Hello, World!", font, brush, 100, 150);
}
// Save the image to a file
string fileName = "generated_image.png";
image.Save(fileName);
Console.WriteLine($"Image saved to {fileName}");
}
}
In this example, a new Bitmap object is created with the desired width and height. Then, a Graphics object is obtained from the image, which allows you to draw on it. You can use various methods and properties of the Graphics object to draw shapes, text, and more on the image. Finally, the image is saved to a file using the Save method of the Bitmap object.
Make sure to add a reference to the System.Drawing assembly in your project for this code to work.