Create an image from text, readwrite it as base64string.

public class ImageGenerator
       /// <summary>
       /// credits to :
       /// </summary>
       /// <param name="imageText">text to render</param>
       /// <returns>generated imaeg</returns>
       public Bitmap CreateTextImage(string imageText)
           Bitmap outputImage = new Bitmap(1, 1);
           int imageWidth, imageHeight = 0;

           //setup the font
           Font imageFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);        

           //using graphic to mesure text and create again the image with the correct size 
           Graphics imageGraphics = Graphics.FromImage(outputImage);
           imageWidth = (int)imageGraphics.MeasureString(imageText, imageFont).Width;
           imageHeight = (int)imageGraphics.MeasureString(imageText, imageFont).Height;
           outputImage = new Bitmap(outputImage, new Size(imageWidth, imageHeight));
           //manage colors
           imageGraphics = Graphics.FromImage(outputImage);

           imageGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
           imageGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
           //imageGraphics.DrawString(imageText, imageFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0, stringFormat);
           imageGraphics.DrawString(imageText, imageFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);

           return outputImage;             

encoding methods:

public static string ConvertImageToBase64String(Image outputImage)
           string result;
           using (MemoryStream memoryStream = new MemoryStream())
               //Retrieving image bytes
               outputImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
               //byte[] imageBytes = memoryStream.ToArray();

               result = Convert.ToBase64String(memoryStream.ToArray());

               //converting bytes in a base64 string
           return result;

       public static Image ConvertBase64StringToImage(string encodedImage)
           //revert base64 string into byte array
           byte[] imageBytes = Convert.FromBase64String(encodedImage);
           Image outputImage;      
           MemoryStream memoryStream = new MemoryStream(imageBytes);
           outputImage = Image.FromStream(memoryStream);

           return outputImage;         

file access methods:

public static void WriteStringToFile(string outputText, string filePath)
           using (StreamWriter writer = new StreamWriter(filePath))

       public static Image ReadEncodedImageFromFile(string filePath)
           string encodedImage;
            //open a streamreader and read the encoded image from the file
           StreamReader reader = File.OpenText(filePath);
           encodedImage = reader.ReadToEnd();

           Image resultImage = ConvertBase64StringToImage(encodedImage);

           return resultImage;

1 Comment

Gli esempi di codice riportati sono molti chiari, e scritti bene
complimenti a chi li ha implementati.
Cmq sia a mio avviso,
sono molto scettico riguardo l’uso di queste tecnologie.
Spero che il mio giudizio sia stato costruttivo.

Leave a Reply

Your email address will not be published. Required fields are marked *