c realizes the transformation of Chinese characters into jpg images with a field grid background

  • 2020-06-15 10:05:46
  • OfStack


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
namespace  Text picture generation program 
{
    static class Program
    {
        /// <summary>
        ///  The main entry point to the application. 
        /// </summary>
       static void checkdir()
        {
            string path="C:\\Users\\Default\\tran";
            if (!Directory.Exists(path))// Determine if the directory exists 
            {
                Directory.CreateDirectory(path);
            }
        }
        [STAThread]
        static void Main()
        {
            Program.checkdir();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }

    }
    class Program1
    {
        private float hor=0.46f;//
        public static string src = null;
        public void setting(float[] z)
        {
            this.hor = z[5];// Text proportion 

            // Line scale 
            // Dotted line ratio 
            // The thickness ratio 
            // Upper and lower position 
            // About the position  
            // Border border   bool
        }
        public void CreateImage(string lf,string content,int w,Color gezi,Color bg,Color ziti_a,Color ziti_b,string ziti,bool flag_b)
        {

            Font font;
            // create 1 A bitmap object 
            Bitmap image = new Bitmap(w, w);
            // create Graphics
            Graphics g = Graphics.FromImage(image);
            try
            {
                // Clear the background color of the image 
                g.Clear(bg);
                if (!flag_b)
                    font = new Font(ziti, w * 0.65f);
                else
                    font = new Font(ziti, w * 0.65f, (FontStyle.Bold));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), ziti_a, ziti_b, 4.0f, true);
                // Draw the border line of the picture 
                g.DrawRectangle(new Pen(gezi, w * 0.007f), w * 0.03f, w * 0.03f, w * 0.94f, w * 0.94f);
                // Draw a dotted line 
                Pen pen1 = new Pen(gezi, w * 0.007f);
                pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
                pen1.DashPattern = new float[] { w * 0.012f, w * 0.008f };
                g.DrawLine(pen1, w * 0.03f, w * 0.03f, w * 0.97f, w * 0.97f);
                g.DrawLine(pen1, w * 0.97f, w * 0.03f, w * 0.03f, w * 0.97f);
                g.DrawLine(pen1, w * 0.03f, w * 0.5f, w * 0.97f, w * 0.5f);
                g.DrawLine(pen1, w * 0.5f, w * 0.03f, w * 0.5f, w * 0.97f);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                g.DrawString(content, font, brush, w*hor, w * 0.63f, sf);
                src = lf + content + ".jpg";

                image.Save(src);

               
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

    }

}


Related articles: