您的位置:首页 > 编程语言 > C#

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

2016-03-21 13:35 836 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: