﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Text;
using System.IO;

namespace text2image
{
    static class text2image
    {
        private static string cpFlag="",cpTextFile="",cpImageFile="";


        private static void ReadMe() 
        {
            MessageBox.Show(
            "text2image 1 ชื่อไฟล์.txt ชื่อไฟล์.jpg ---> Convert .txt To .jpg แบบตัดบรรทัดว่าง" + System.Environment.NewLine
            + "text2image 2 ชื่อไฟล์.txt ชื่อไฟล์.jpg ---> Convert .txt To .jpg แบบไม่ตัดบรรทัดว่าง" + System.Environment.NewLine
            );
        }


        static void Pok_Mess(string cfWord)
        {
            MessageBox.Show(cfWord + System.Environment.NewLine
            + "" + System.Environment.NewLine
            + "" + System.Environment.NewLine
            , "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


        private static Bitmap Convert_Text_to_Image(string txt, string fontname, int fontsize)
        {
            //creating bitmap image
            Bitmap bmp = new Bitmap(1, 1);
            //FromImage method creates a new Graphics from the specified Image.
            Graphics graphics = Graphics.FromImage(bmp);
            // Create the Font object for the image text drawing.
            Font font = new Font(fontname, fontsize);
            // Instantiating object of Bitmap image again with the correct size for the text and font.
            SizeF stringSize = graphics.MeasureString(txt, font);
            bmp = new Bitmap(bmp, (int)stringSize.Width, (int)stringSize.Height);
            graphics = Graphics.FromImage(bmp);
            //Draw Specified text with specified format 
            graphics.DrawString(txt, font, Brushes.Yellow, 0, 0);
            font.Dispose();
            graphics.Flush();
            graphics.Dispose();
            return bmp;     //return Bitmap Image 
        }


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length < 3) { ReadMe(); return; }
            cpFlag = args[0].Trim(); 
            cpTextFile = args[1].Trim();
            cpImageFile = "";
            for (int i = 2; i < args.Length; i++)
            {
                cpImageFile = cpImageFile + args[i];
            }
            if (cpImageFile.Length<5) { cpImageFile = cpImageFile + ".jpg"; }
            if (cpImageFile.Substring(cpImageFile.Length-4)!=".jpg") { cpImageFile = cpImageFile + ".jpg"; }
            if (!File.Exists(cpTextFile)) { Pok_Mess("ไม่พบไฟล์ " + cpTextFile);return;}

            string cWord ="";
            string[] lines = System.IO.File.ReadAllLines(cpTextFile);
            foreach (string line in lines)
            {
                if (line != "" || cpFlag=="2")
                {
                    if (cWord == "") { cWord = line.Trim(); } // บรรทัดแรก 
                    else { cWord = cWord + Environment.NewLine + line.Trim(); }
                }
            }
            Image SaveBitmap = Convert_Text_to_Image(cWord,"Tahoma",24);
            SaveBitmap.Save(cpImageFile, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}




