Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

How to Generate Image using C#

Date- May 31,2023

4480

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.

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page