How to Generate Image using C# | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

How to Generate Image using C#

Date- May 31,2023

2630

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.

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
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 | 1210
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

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts