-
-
Save MrOplus/efd2cdb07ccb03812750908af7e25b92 to your computer and use it in GitHub Desktop.
Converting text to image (png) C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Converting text to image (png). | |
/// </summary> | |
/// <param name="text">text to convert</param> | |
/// <param name="font">Font to use</param> | |
/// <param name="textColor">text color</param> | |
/// <param name="maxWidth">max width of the image</param> | |
/// <param name="path">path to save the image</param> | |
public static void DrawText(String text, Font font, Color textColor,int maxWidth,String path) | |
{ | |
//first, create a dummy bitmap just to get a graphics object | |
Image img = new Bitmap(1, 1); | |
Graphics drawing = Graphics.FromImage(img); | |
//measure the string to see how big the image needs to be | |
SizeF textSize = drawing.MeasureString(text, font,maxWidth); | |
//set the stringformat flags to rtl | |
StringFormat sf = new StringFormat(StringFormatFlags.DirectionRightToLeft); | |
//uncomment the next line for right to left languages | |
//sf.FormatFlags = StringFormatFlags.DirectionRightToLeft; | |
sf.Trimming = StringTrimming.Word; | |
//free up the dummy image and old graphics object | |
img.Dispose(); | |
drawing.Dispose(); | |
//create a new image of the right size | |
img = new Bitmap((int)textSize.Width, (int)textSize.Height); | |
drawing = Graphics.FromImage(img); | |
//Adjust for high quality | |
drawing.CompositingQuality = CompositingQuality.HighQuality; | |
drawing.InterpolationMode = InterpolationMode.HighQualityBilinear; | |
drawing.PixelOffsetMode = PixelOffsetMode.HighQuality; | |
drawing.SmoothingMode = SmoothingMode.HighQuality; | |
drawing.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
//paint the background | |
drawing.Clear(Color.Transparent); | |
//create a brush for the text | |
Brush textBrush = new SolidBrush(textColor); | |
drawing.DrawString(text, font, textBrush, new RectangleF(0, 0, textSize.Width,textSize.Height), sf); | |
drawing.Save(); | |
textBrush.Dispose(); | |
drawing.Dispose(); | |
img.Save(path,ImageFormat.Png); | |
img.Dispose(); | |
} | |
public static Image DrawText(String text, Font font, Color textColor, int maxWidth) | |
{ | |
//first, create a dummy bitmap just to get a graphics object | |
Image img = new Bitmap(1, 1); | |
Graphics drawing = Graphics.FromImage(img); | |
//measure the string to see how big the image needs to be | |
SizeF textSize = drawing.MeasureString(text, font, maxWidth); | |
//set the stringformat flags to rtl | |
StringFormat sf = new StringFormat(StringFormatFlags.DirectionRightToLeft); | |
//uncomment the next line for right to left languages | |
//sf.FormatFlags = StringFormatFlags.DirectionRightToLeft; | |
sf.Trimming = StringTrimming.Word; | |
//free up the dummy image and old graphics object | |
img.Dispose(); | |
drawing.Dispose(); | |
//create a new image of the right size | |
img = new Bitmap((int)textSize.Width, (int)textSize.Height); | |
drawing = Graphics.FromImage(img); | |
//Adjust for high quality | |
drawing.CompositingQuality = CompositingQuality.HighQuality; | |
drawing.InterpolationMode = InterpolationMode.HighQualityBilinear; | |
drawing.PixelOffsetMode = PixelOffsetMode.HighQuality; | |
drawing.SmoothingMode = SmoothingMode.HighQuality; | |
drawing.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; | |
//paint the background | |
drawing.Clear(Color.Transparent); | |
//create a brush for the text | |
Brush textBrush = new SolidBrush(textColor); | |
drawing.DrawString(text, font, textBrush, new RectangleF(0, 0, textSize.Width, textSize.Height), sf); | |
drawing.Save(); | |
textBrush.Dispose(); | |
drawing.Dispose(); | |
return img; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment