C# 图片添加旋转字体文字,实现图片装逼功能

public ActionResult View1()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "font\\向佳红毛笔行书.ttf";  //选择字体文件
            string ItemPath = AppDomain.CurrentDomain.BaseDirectory + "Image\\2.jpg";   //选择图片
            string str = Request["name"];   //获取 网页网址 ?name=  也就是名字
            if (System.IO.File.Exists(ItemPath))
            {
                System.IO.MemoryStream img = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(ItemPath));
                Bitmap bmp = new Bitmap(img);
                Graphics g = Graphics.FromImage(bmp);
                //读取字体文件并添加字体文件             
                PrivateFontCollection pfc = new PrivateFontCollection();
                pfc.AddFontFile(path);
                Font font = new Font(pfc.Families[0], 38);
                SolidBrush sbrush = new SolidBrush(Color.FromArgb(205,0,0,0));  
                PointF point = new PointF(317, 790);
                // 绘制围绕点旋转的文本
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                Matrix mtxSave = g.Transform;

                Matrix mtxRotate = g.Transform;
                mtxRotate.RotateAt(5f, point); // 旋转角度和平移
                g.Transform = mtxRotate;

                g.DrawString(str, font, sbrush, point, format); //处理图片, (名字,字体样式,颜色,文字在图片上的位置,旋转角度
                MemoryStream ms = new MemoryStream();
                //if (System.IO.File.Exists(ItemPath))
                //{
                //    System.IO.File.Delete(TmpPath);
                //}
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                g.Dispose();
                bmp.Dispose();
                return File(ms.ToArray(), "image/jpeg");
            }
            else
            {
                return View();
            }

        }
demo下载: 链接: http://pan.baidu.com/s/1bockKDT 密码: bxxm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值