Thursday, June 25, 2020

Creating Images from Text in C#

Sometimes you might need to create images from text. Following method can be used to do that.

 public static Image DrawText(String text, Font font, Color textColor, Color backColor)  
     {        
       Image img = new Bitmap(1, 1);  
       Graphics drawing = Graphics.FromImage(img);  
       SizeF textSize = drawing.MeasureString(text, font);  
       img.Dispose();  
       drawing.Dispose();  
       img = new Bitmap((int)textSize.Width, (int)textSize.Height);  
       drawing = Graphics.FromImage(img);  
       drawing.Clear(backColor);  
       drawing.SmoothingMode = SmoothingMode.AntiAlias;  
       drawing.InterpolationMode = InterpolationMode.HighQualityBicubic;  
       drawing.PixelOffsetMode = PixelOffsetMode.HighQuality;  
       drawing.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;  
       Brush textBrush = new SolidBrush(textColor);  
       drawing.DrawString(text, font, textBrush, 0, 0);  
       drawing.Save();  
       textBrush.Dispose();  
       drawing.Dispose();  
       return img;  
     }  

Such scenario can be applied to a website like below, The logo is created from a text there.




Happy coding.
Copy this code...and try

No comments:

Post a Comment