实现在图片指定位置添加汉字

实现在图片指定的位置添加汉字



/*
 * => 在图片的指定位置添加 =! 汉字
 * =>002
 * =>2015/2/28
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//添加需要的类,右键项目名->添加引用
using System.Drawing.Imaging;
using System.Drawing.Design;
using System.Drawing;


using System.IO;
using System.Text;


namespace memDC
{
    class Program
    {
        static void Main(string[] args)
        {

            int xpos = 150;  // 写入汉字的位置:位置1
            int ypos = 635;
            int xpos2 = 125; // 位置2
            int ypos2 = 855;

            StreamReader sr = new StreamReader("word.txt",Encoding.Default);
            String line;


            for (int count = 1; count <=9; count++)
            {
                string img = "c:\\out\\" + count + ".jpg";
                string res = count + ".jpg";
                Console.WriteLine(res);

                //读取指定图片
                Image image = Image.FromFile(img);
                Bitmap bmap = new Bitmap(image, image.Width, image.Height);
                Graphics g = Graphics.FromImage(bmap);

                SolidBrush drawBrush = new SolidBrush(Color.Black);
                  //Font drawFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Millimeter);
                Font drawFont = new Font(FontFamily.GenericSerif, 8, FontStyle.Bold, GraphicsUnit.Millimeter);

                //获取要写入的汉字
                if( (line=sr.ReadLine())== null)
                {
                    break;
                }
                g.DrawString(line, drawFont, drawBrush, xpos, ypos);
                g.DrawString(line, drawFont, drawBrush, xpos2, ypos2);

                // 写入图片汉字,并保存图片
                Image smBmap;
                smBmap = bmap.GetThumbnailImage(bmap.Width, bmap.Height, null, System.IntPtr.Zero);
                smBmap.Save(res, System.Drawing.Imaging.ImageFormat.Jpeg);
                smBmap.Dispose();

                //释放内存空间
                bmap.Dispose();
                image.Dispose();
                g.Dispose();
                bmap = null;
                smBmap = null;
            }
            sr.Close();
            Console.ReadLine();
        }
    }
}


转载于:https://my.oschina.net/badboy2/blog/380935

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值