1
我现在有这被显示给用户的图像,我想根据传入的两个参数动态文本添加到该图像。添加动态文本图像
我的问题是,当我一步通过代码,它似乎都工作正常,但是当我在下面的代码运行后在屏幕上看到图像时,它没有文本。
下面的代码我的当前设置:
public ActionResult GenerateImage(string savingAmount, string savingDest)
{
// Hardcoding values for testing purposes.
savingAmount = "25,000.00";
savingDest = "Canada";
PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);
Image imgBackground = Image.FromFile(Server.MapPath("~/assets/img/fb-share.jpg"));
int phWidth = imgBackground.Width; int phHeight = imgBackground.Height;
Bitmap bmBackground = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmBackground.SetResolution(72, 72);
Graphics grBackground = Graphics.FromImage(bmBackground);
Bitmap bmWatermark;
Graphics grWatermark;
bmWatermark = new Bitmap(bmBackground);
bmWatermark.SetResolution(imgBackground.HorizontalResolution, imgBackground.VerticalResolution);
grWatermark = Graphics.FromImage(bmWatermark);
grBackground.SmoothingMode = SmoothingMode.AntiAlias;
// Now add the dynamic text to image
using (Graphics graphics = Graphics.FromImage(imgBackground))
{
using (Font arialFont = new Font("Arial", 10))
{
grWatermark.DrawString(savingAmount, arialFont, Brushes.White, firstLocation);
grWatermark.DrawString(savingDest, arialFont, Brushes.White, secondLocation);
}
}
imgBackground.Save(Response.OutputStream, ImageFormat.Png);
Response.ContentType = "image/png";
Response.Flush();
Response.End();
return null;
}
如前所述运行此代码后,我便看到在浏览器但文本在图像上显示的图像,任何人都可以看到/提示什么可能导致这个问题?
乍一看,代码看起来不错;它可以是__browser refresh__问题吗?你能检查文件系统中的结果吗?另外:__did__在__purpose__上从'jpg'改为'png',对不对? – TaW
@David是对的,你应该使用两个Graphics对象! (一个没有被处理!) – TaW