Skip to content

Instantly share code, notes, and snippets.

@hepper
Last active August 16, 2018 16:14
Show Gist options
  • Save hepper/91ac720d43af13f82f112e0dc0bb94d7 to your computer and use it in GitHub Desktop.
Save hepper/91ac720d43af13f82f112e0dc0bb94d7 to your computer and use it in GitHub Desktop.
using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.IO;
public class TextImageGenerator
{
private Color TextColor { get; set; }
private Color BackgroundColor { get; set; }
private Font Font { get; set; }
private int Padding { get; set; }
private int FontSize { get; set; }
public TextImageGenerator(Color textColor, Color backgroundColor, string font, int padding, int fontSize)
{
TextColor = textColor;
BackgroundColor = backgroundColor;
Font = new Font(font, fontSize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);;
Padding = padding;
FontSize = fontSize;
}
public TextImageGenerator()
: this(Color.Orange, Color.Black, "Arial", 20, 20)
{ }
public Bitmap CreateBitmap(string text)
{
// Create graphics for rendering
Graphics retBitmapGraphics = Graphics.FromImage(new Bitmap(1, 1));
// measure needed width for the image
var intWidth = (int)retBitmapGraphics.MeasureString(text, Font).Width;
// measure needed height for the image
var intHeight = (int)retBitmapGraphics.MeasureString(text, Font).Height;
// Create the bitmap with the correct size and add padding
Bitmap retBitmap = new Bitmap(intWidth + Padding, intHeight + Padding);
// Add the colors to the new bitmap.
retBitmapGraphics = Graphics.FromImage(retBitmap);
// Set Background color
retBitmapGraphics.Clear(BackgroundColor);
retBitmapGraphics.SmoothingMode = SmoothingMode.AntiAlias;
retBitmapGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
retBitmapGraphics.DrawString(text, Font, new SolidBrush(TextColor), Padding / 2, Padding / 2);
// flush the changes
retBitmapGraphics.Flush();
return (retBitmap);
}
public string CreateBase64Image(string text, ImageFormat imageFormat)
{
var bitmap = CreateBitmap(text);
var stream = new System.IO.MemoryStream();
// save into stream
bitmap.Save(stream, imageFormat);
// convert to byte array
var imageBytes = stream.ToArray();
// convert to base64 string
return Convert.ToBase64String(imageBytes);
}
public void SaveAsJpg(string filename, string text)
{
var bitmap = CreateBitmap(text);
bitmap.Save(filename, ImageFormat.Jpeg);
}
public void SaveAsPng(string filename, string text)
{
var bitmap = CreateBitmap(text);
bitmap.Save(filename, ImageFormat.Png);
}
public void SaveAsGif(string filename, string text)
{
var bitmap = CreateBitmap(text);
bitmap.Save(filename, ImageFormat.Gif);
}
public void SaveAsBmp(string filename, string text)
{
var bitmap = CreateBitmap(text);
bitmap.Save(filename, ImageFormat.Bmp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment