Adjusting column widths based on content fails on Windows Azure Websites because it seems that the functionality provided by [System.Windows.Forms.TextRenderer](http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer.aspx), which is used for measuring string widths, is missing. TextRenderer returns arbitrary output (width > 1,000,000 for the string "hello") instead of real values.
The issue can be resolved by using GDI+ instead of GDI, hence using [System.Drawing.Graphics.MeasureString](http://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurestring.aspx).
The following code snippet demonstrates a rough idea how text measuring using GDI+ could be implemented. Note however that always bold fonts are assumed and that rich text is not handled appropriately.
```
public static void AdjustToContentsGDIPlus(this IXLColumn column)
{
using (var bitmap = new Bitmap(1, 1))
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
var fontCache = new Dictionary<IXLFontBase, Font>();
var width = column.CellsUsed()
.Select(c => new {Font = GetFont(c.Style.Font, fontCache), Value = c.GetFormattedString()})
.Max(c => MeasureText(graphics, c.Value, c.Font));
column.Width = width;
}
}
private static Font GetFont(IXLFontBase fontBase, IDictionary<IXLFontBase, Font> fonts)
{
Font font;
if (!fonts.TryGetValue(fontBase, out font))
{
font = new Font(fontBase.FontName, (float) fontBase.FontSize, FontStyle.Bold);
fonts.Add(fontBase, font);
}
return font;
}
private static double MeasureText(Graphics graphics, string text, Font font)
{
var size = graphics.MeasureString(text, font, Int32.MaxValue, StringFormat.GenericTypographic);
// calculation taken from FontBaseExtensions.GetWidth method
var width = (((size.Width / (double)7) * 256) - (128 / 7)) / 256;
width = (double) Decimal.Round((decimal)width + 0.2M, 2);
return width;
}
```
Comments: ** Comment from web user: paaland **
The issue can be resolved by using GDI+ instead of GDI, hence using [System.Drawing.Graphics.MeasureString](http://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurestring.aspx).
The following code snippet demonstrates a rough idea how text measuring using GDI+ could be implemented. Note however that always bold fonts are assumed and that rich text is not handled appropriately.
```
public static void AdjustToContentsGDIPlus(this IXLColumn column)
{
using (var bitmap = new Bitmap(1, 1))
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
var fontCache = new Dictionary<IXLFontBase, Font>();
var width = column.CellsUsed()
.Select(c => new {Font = GetFont(c.Style.Font, fontCache), Value = c.GetFormattedString()})
.Max(c => MeasureText(graphics, c.Value, c.Font));
column.Width = width;
}
}
private static Font GetFont(IXLFontBase fontBase, IDictionary<IXLFontBase, Font> fonts)
{
Font font;
if (!fonts.TryGetValue(fontBase, out font))
{
font = new Font(fontBase.FontName, (float) fontBase.FontSize, FontStyle.Bold);
fonts.Add(fontBase, font);
}
return font;
}
private static double MeasureText(Graphics graphics, string text, Font font)
{
var size = graphics.MeasureString(text, font, Int32.MaxValue, StringFormat.GenericTypographic);
// calculation taken from FontBaseExtensions.GetWidth method
var width = (((size.Width / (double)7) * 256) - (128 / 7)) / 256;
width = (double) Decimal.Round((decimal)width + 0.2M, 2);
return width;
}
```
Comments: ** Comment from web user: paaland **
This is a huge issue for me. This single issue is preventing me from going live on Azure.