按照竖版,在图片上打字

来源:互联网 发布:淘宝怎么看历史最低价 编辑:程序博客网 时间:2024/04/27 21:51


项目中需要对一批古籍做处理,在已经做好的封面底图上,打上不同的书名

 

空白封面

 

处理好的封面

 

因为是竖版,还要考虑文字的位置,并且每列文字满了以后,还要自动换到下一列。

具体代码如下:

 

//待写入文字            string text = "本草纲目";            //待打字的原图            string basePic = @"d:\book.jpg";            //处理好的图            string buildPath = @"d:\book_ok.jpg";            //定义字体和颜色            Color fontColor = System.Drawing.Color.White;            Font font = new Font("微软雅黑", 16);            var imageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;            int levelCount = 24;//每列的宽度            int x = 5;//当前文字X轴坐标            int y = 5;//当前文字Y轴坐标            Bitmap bitMap = new Bitmap(basePic);            Graphics graphics = Graphics.FromImage(bitMap);            //开始在图片上打字,此处要为每个字定位,写满10个字,要换下一列,给X加levelCount            graphics.DrawString(text[0].ToString(), font, new SolidBrush(fontColor), new PointF(x, y));            for (int i = 1; i < text.Length; i++)            {                if (i % 10 == 0)                {                    x += levelCount; y = 5;                }                else                {                    y += levelCount;                }                string c = text[i].ToString();                graphics.DrawString(c, font, new SolidBrush(fontColor), new PointF(x, y));            }            bitMap.Save(buildPath, imageFormat);            //清理资源                        graphics.Dispose();            bitMap.Dispose();