How to Generate Image using C# | Code2night.com
Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
31 May
2023

How to Generate Image using C#

360

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.

  • |
  • How to Generate Image using C#

Comments

Follow Us On Social Media - Like Us On Facebook

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 New Subscribers!

Subscribe and Click on Verify and Download for download link

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

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading

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 © 2023 by Code2night. All Rights Reserved

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