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

MS Excel Reader with No dependancy in C# - Reading file horizontally

This is a cool nuget package that you can use to read excel files into dataset just like how you query the database. Very simple and no dependancy. What you need is to install the add the following 2 nuget packages into your solution.



These are the URLs,
https://www.nuget.org/packages/ExcelDataReader/

https://www.nuget.org/packages/ExcelDataReader.DataSet/

Then you can read the file as below. Note that this code will read your file horizontally. Meaning column by column.

 try {  
  OpenFileDialog of = new OpenFileDialog(); of .Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"; of .ShowDialog();  
  var FilePath = of .FileName;  
  if (File.Exists(FilePath)) {  
  try {  
   using(var stream = File.Open(FilePath, FileMode.Open, FileAccess.Read)) {  
   using(var reader = ExcelReaderFactory.CreateReader(stream)) {  
    var result = reader.AsDataSet();  
    if (result != null && result.Tables != null && result.Tables.Count > 0) {  
    rowCountMultiple = result.Tables[0].Rows.Count;  
    colCountMultiple = result.Tables[0].Columns.Count;  
    List < string > valueList;  
    string token, value;  
    listTokensForPageMultiple = new Dictionary < int, List < string >> ();  
    for (int c = 0; c < colCountMultiple; c++) {  
     valueList = new List < string > ();  
     token = result.Tables[0].Rows[0][c].ToString();  
     for (int r = 0; r < rowCountMultiple; r++) {  
     value = result.Tables[0].Rows[r][c].ToString();  
     valueList.Add(value);  
     }  
     listTokensForPageMultiple.Add(c, valueList);  
    }  
    }  
   }  
   }  
  } catch (Exception ex) {  
   Common.Error(ex.Message, ex);  
  } finally {  
   GC.Collect();  
  }  

Copy and paste this code and try.
Happy coding...