图片上绘制文字换行处理

 protected void Page_Load(object sender, EventArgs e)
        {
            CreatePicture("2010", "我是中国人们的儿子我深深的爱着我的祖国和人民", "我是中国人们的儿子我深深的爱着我的祖国和人民", @"C:\Users\wangyanfei\Pictures\1277702967234.jpg");
        }
        public void CreatePicture(string year, string chubpc, string kanm, string imgUrl)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(imgUrl);//获取图片路径
            Bitmap bmpImage = new Bitmap(image, 173, 228);
            Graphics graphic = Graphics.FromImage(bmpImage);
         
            Brush whiteBrush = new SolidBrush(Color.White);   //白笔刷,画文字用
            Brush blackBrush = new SolidBrush(Color.Black);   //黑笔刷,画文字用
          

            using (Font font1 = new Font("宋体", 12, FontStyle.Bold, GraphicsUnit.Point))
            {
                RectangleF rectF1 = new RectangleF(20, 80, 153, 50);
                graphic.DrawString(kanm, font1, blackBrush, rectF1);//建立一个矩形 让其在里面换行
                RectangleF rectF2 = new RectangleF(120, 200, 53, 50);
                graphic.DrawString(year, font1, blackBrush,  rectF2);
                RectangleF rectF3 = new RectangleF(120, 180, 53, 50);
                graphic.DrawString(chubpc, font1, blackBrush,  rectF3);
            }

          

            MemoryStream ms = new MemoryStream();
            bmpImage.Save("D:/1.jpg", ImageFormat.Jpeg);
            //保存为Jpg类型
            //return ms;
        }
 
string text1 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
{
    RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
    e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
    e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
}
 
string text2 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
using (Font font2 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
{
    Rectangle rect2 = new Rectangle(30, 10, 100, 122);

    // Specify the text is wrapped.
    TextFormatFlags flags = TextFormatFlags.WordBreak;
    TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags);
    e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rect2));

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值