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

来源:互联网 发布:ar java技术实现 编辑:程序博客网 时间:2024/06/06 17:18
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
原创粉丝点击