I’m creating some images of text in code that need to reflect a CSS Font as closely as possible. The CSS class looks like this:
.font
{
letter-spacing: -0.7px;
font-family: segoe wp,segoe ui,verdana,arial,helvetica,sans-serif;
font-size: 1.15em;
padding: 10px;
}
My image code looks like this:
using (var helper = new Bitmap(1, 1))
using (var gHelper = Graphics.FromImage(helper))
using (var font = new Font("Segoe WP", 1.15f, GraphicsUnit.Point))
using (var brush = new SolidBrush(Color.White))
{
var size = gHelper.MeasureString(concept, font);
using (var image = new Bitmap((int)size.Width + _padding, (int)size.Height + _padding))
{
using (var g = Graphics.FromImage(image))
{
g.Clear(Color.Black);
g.DrawString(concept, font, brush, (float)_padding / 2, (float)_padding / 2);
}
var converter = new ImageConverter();
var b = (byte[])converter.ConvertTo(image, typeof(byte[]));
return File(b, _contentType);
}
}
The images that are being produced by this code are VERY small.
The letter spacing I’m guessing will require that I make a loop and draw each letter individually, but how do I work with EMs inside of C#?
In my opinion you can’t work with EMs, because they’re not fixed.
Read this part (extracted from http://webdesign.about.com/od/typemeasurements/qt/how-big-is-an-em.htm):
So you can try:
knowing from the very beginning that this is only an approximation!!