1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| // ***************************************************************************************
private static Bitmap ToImage(string curStr, Font font, Color color, Color BackColor)
{
if (curStr == null) return null;
if (font == null) return null;
if (color==null) return null;
if (BackColor == null) return null;
StringFormat format = new StringFormat();
SolidBrush LgdBrush = new SolidBrush(color);
format.Alignment = StringAlignment.Center;
//
TextFormatFlags flags = TextFormatFlags.LeftAndRightPadding;
// Declare a proposed size with dimensions set to the maximum integer value.
Size proposedSize = new Size(int.MaxValue, int.MaxValue);
SizeF szTxt=TextRenderer.MeasureText(curStr, font, proposedSize, flags);
szTxt.Width += 4;
szTxt.Height += 2;
// +2 pour cadre eventuel
Bitmap bmpTxt = new Bitmap((int)szTxt.Width, (int)szTxt.Height);
Graphics txtG = Graphics.FromImage(bmpTxt);
txtG.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle cellarea = new Rectangle(0, 0, (int)szTxt.Width, (int)szTxt.Height);
SolidBrush BackColorBr = new SolidBrush(Color.FromArgb(1, 255, 255, 255));
txtG.FillRectangle(BackColorBr, cellarea);
Color cc = Color.Black;
TextRenderer.DrawText(txtG, curStr, font, cellarea, cc, flags);
// txtG.DrawString(curStr, font, LgdBrush, cellarea, format);
txtG.Dispose();
return bmpTxt;
} |
Partager