2017-08-14

Convert Txt to Image

https://forums.asp.net/t/1832136.aspx?how+to+convert+text+to+image+in+c+


        public static void LoopingFolders()
        {
            Console.WriteLine("Please enter target Folder Path?");
            string basePath = Console.ReadLine();
         
            DirectoryInfo di = new DirectoryInfo(basePath);
            foreach (var dir in di.GetDirectories())
            {
                foreach(var file in dir.GetFiles("*.txt"))
                {
                    string content = File.ReadAllText(file.FullName);
                    string name = file.FullName;
                    SaveTxtAsImage(content, name);
                }
            }
        }
        public static void SaveTxtAsImage(string content, string targetFileName)
        {

       

       
            Bitmap bitmap = new Bitmap(1, 1);
            Font font = new Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
            Graphics graphics = Graphics.FromImage(bitmap);
            int width =600;
            int height = 800;

            bitmap = new Bitmap(bitmap, new Size(width, height));
            graphics = Graphics.FromImage(bitmap);
            graphics.Clear(Color.LightYellow);
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.TextRenderingHint = TextRenderingHint.AntiAlias;

            graphics.DrawString(content, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 20);
       
            graphics.Flush();
            graphics.Dispose();

            string fileName = targetFileName + ".jpg";
            bitmap.Save( fileName, ImageFormat.Jpeg);


        }

No comments:

Post a Comment

Setup VNC on Ubuntu 20.04

  sudo apt update sudo apt install xfce4 xfce4-goodies sudo apt install tigervnc-standalone-server sudo apt install tightvncserver vncserver...