Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // You know you're a programmer when this is how
- // You get some cheap chess icons...
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace StringToImage
- {
- class Program
- {
- static void Main(string[] args)
- {
- for (int i = 0; i < 12; i++)
- TextToImage(((char)('\u2654' + i)).ToString(), 256, 256, i > 5 ? Color.Black : Color.White)
- .Save("C:\\CHESS\\" + "WB"[i / 6] + "KQRBNP"[i % 6] + ".png", ImageFormat.Png);
- }
- public static Bitmap TextToImage(string text, int height = 40, int width = 150, Color? textColor = null)
- {
- textColor = textColor ?? Color.White;
- Bitmap bmp = new Bitmap(width, height);
- using (Graphics gr = Graphics.FromImage(bmp))
- {
- gr.FillRectangle(new SolidBrush(Color.Transparent), 0, 0, width, height);
- Font fn = new Font("Copperplate Gothic", 1, FontStyle.Regular);//"Comic Sans MS"
- for (int fnSize = 1; !string.IsNullOrWhiteSpace(text); fnSize++)
- {
- var newFn = new Font(fn.FontFamily, fnSize, fn.Style);
- var size = gr.MeasureString(text.Replace(' ', '_'), newFn);
- if (size.Height < height && size.Width < width) fn = newFn;
- else break;
- }
- var offset = bmp.Size - gr.MeasureString(text.Replace(' ', '_'), fn);
- gr.DrawString(text, fn, new SolidBrush(textColor.Value), offset.Width / 2, offset.Height / 2);
- return bmp;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement