62,046
社区成员
发帖
与我相关
我的任务
分享
public static void CreateImg(string text, string filePath)
{
int width = Convert.ToInt32(text.Length * 10);
Bitmap img = new Bitmap(width, 20);
Graphics gfc = Graphics.FromImage(img);
gfc.Clear(Color.White);
Font font = new Font("arial", 10, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, img.Width, img.Height),
Color.DarkOrchid, Color.Red, 1.5f, true);
gfc.DrawString(text, font, brush, 3, 2);
gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);
img.Save(filePath);
//将图像添加到页面
//MemoryStream ms = new MemoryStream();
//img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//byte[] _a = ms.ToArray();
img.Dispose();
//ms.Dispose();
//return _a;
}
var imagePath = @"~/Images/test.png";
Utils.CreateImg("Hello Tester", Server.MapPath(imagePath));
this.Image1.ImageUrl = imagePath;
string path = "1.jpg";
string text = "天天开心!";
string addtext = "文字转为图片结果为:";
Bitmap bitmap = new Bitmap(500, 100);
Graphics g = Graphics.FromImage(bitmap);
Font f = new Font("宋体", 14);
SizeF size = g.MeasureString(addtext + text, f);
SolidBrush brush = new SolidBrush(Color.White);
g.Clear(Color.Black);
g.DrawString(addtext + text, f, brush, bitmap.Width - size.Width, bitmap.Height - size.Height);
bitmap.Save(Server.MapPath(path));
Image1.ImageUrl = path;