Mohanapriya R Mohanapriya R
Updated date Aug 06, 2024
In this article, we will learn how to convert strings into images in C#.

How to Convert Strings to Image in C#:

Below the program we will convert the strings to Image in C# as shown below,

using System;
using System.Drawing;

class StringToImageConverter
{
    static void Main()
    {
        // Input string
        string inputString = "Hello, TechieHook!";

        // Create a bitmap with fixed dimensions
        Bitmap image = new Bitmap(200, 100);

        // Create a Graphics object from the image
        using (Graphics g = Graphics.FromImage(image))
        {
            // Set the background color
            g.Clear(System.Drawing.Color.White);

            // Set the font and color for the text
            Font font = new Font("Arial", 14);
            SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);

            // Draw the string on the image
            g.DrawString(inputString, font, brush, new PointF(10, 10));
        }

        // Save the image to a file
        image.Save("image-output.png");

        Console.WriteLine("Image generated and saved as 'image-output.png'");
    }
}

The above C# program initializes a string "Hello, TechieHook!", creates a bitmap, and uses the Graphics class to draw the string onto the image. Then the image is saved to a file named "image-output.png"

Output:

After executing the program, you will find a file named "image-output.png" in the same directory (Bin folder). 

Comments (0)

There are no comments. Be the first to comment!!!