Create an image from text (Updated)

I’ve done a few changes and some additions on the code of the previous post.

The CreateTextImage now prints the text at the center of the image, this is done using the Alignment property of the StringFormat class:

public Bitmap CreateTextImage(string imageText)
{
    Bitmap outputImage = new Bitmap(1, 1);
    int imageWidth, imageHeight = 0;

    //setup the font
    Font imageFont = new Font("Arial", 25, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    stringFormat.LineAlignment = StringAlignment.Center;

    //using graphic to mesure text and create again the image with the correct size 
    Graphics imageGraphics = Graphics.FromImage(outputImage);
    imageWidth = (int)imageGraphics.MeasureString(imageText, imageFont).Width + 80;
    imageHeight = (int)imageGraphics.MeasureString(imageText, imageFont).Height + 80;
    outputImage = new Bitmap(outputImage, new Size(imageWidth, imageHeight));

    //manage colors
    imageGraphics = Graphics.FromImage(outputImage);

    //background
    imageGraphics.Clear(Color.White);
    imageGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    imageGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
           imageGraphics.DrawString(imageText, imageFont, new SolidBrush(Color.Black), new RectangleF(0,0,imageWidth,imageHeight), stringFormat);
    imageGraphics.Flush();

    return outputImage;
}

Some extensions methods for the Bitmap class, adding the chance to save te image as a JPEG and set the quality of the output:

public static class BitmapExtensions
{
    public static void SaveJPG100(this Bitmap bmp, string filename)
    {
        EncoderParameters encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
        bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);
    }

    public static void SaveJPG100(this Bitmap bmp, Stream stream)
    {
        EncoderParameters encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
        bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);
    }

    public static void SaveJPGCustomQuality(this Bitmap bmp, string filename, Int64 quality)
    {
        EncoderParameters encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        bmp.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters);
    }

    public static void SaveJPGCustomQuality(this Bitmap bmp, Stream stream, Int64 quality)
    {
        EncoderParameters encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);
    }

    public static ImageCodecInfo GetEncoder(ImageFormat format)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s