在现有图像上书写文字

时间:2015-02-14 11:03:19

标签: c# asp.net image

我正在尝试编写一些ASP.NET页面,该页面将获取现有图像并向其中添加文本。然后我希望图像(带有文本)显示在页面中,就像它是一个.png页面,所以它可以加载到论坛等。 这是我现在的代码:

Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath("~/Images/sigbg.png"));
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
graphicImage.DrawString("Test", new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold), SystemBrushes.WindowText, 0,0);
graphicImage.DrawArc(new Pen(Color.Red, 3), 0, 0, 150, 50, 0, 360);
Response.ContentType = "image/png";
bitMapImage.Save(Response.OutputStream, ImageFormat.Png);
graphicImage.Dispose();
bitMapImage.Dispose();

不幸的是,它不会将文本添加到图像并将其另存为原始图像。然后页面显示“新”图像,但没有文本。怎么解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的代码是正确的,并在左上角输出“test”和一个椭圆。确保未缓存页面/图像。例如,如果您将代码放在aspx页面中,那么尝试使用随机查询调用该页面,即“image.aspx?12345”或在浏览器中清除缓存。

您也可以尝试添加以下代码以避免缓存

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
相关问题