欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#实现把汉字转为带田字格背景的jpg图片

程序员文章站 2024-02-24 10:54:04
复制代码 代码如下:using system;using system.collections.generic;using system.linq;using system...

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using system.windows.forms;
using system.drawing;
using system.io;

namespace 文字图片生成程序
{
    static class program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
       static void checkdir()
        {
            string path="c:\\users\\default\\tran";
            if (!directory.exists(path))//判断目录是否存在
            {
                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];//文字比例


            //线条比例
            //虚线比例
            //粗细比例
            //上下位置
            //左右位置
            //边框边界  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;
            //创建一个位图对象
            bitmap image = new bitmap(w, w);
            //创建graphics
            graphics g = graphics.fromimage(image);
            try
            {
                //清空图片背景颜色
                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);
                //画图片的边框线
                g.drawrectangle(new pen(gezi, w * 0.007f), w * 0.03f, w * 0.03f, w * 0.94f, w * 0.94f);
                //画虚线
                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();
            }
        }

    }

}