我正在引用一个 ashx 文件来显示在运行时生成的图像:
<img alt="" style="height: 200px; width: 450px;"
id="Img1" src="<%= Url.Content ("~/Helpers/GetImage.ashx?side=Front") %>" />
下面是创建图像的代码。但我担心这不是正确的方法。我应该改用返回 ImageResult 的控制器吗?
if (!string.IsNullOrEmpty(context.Request.Params["side"]))
{
string ImageType = string.Empty;
if (context.Request.Params["side"].Equals("Front"))
{
ImageType="FrontJpegBase64";
}
else
{
ImageType = "BackJpegBase64";
}
Log.Info(context.Session["FrontBase64"].ToString());
byte[] imageByteArray = System.Convert.FromBase64String(context.Session[ImageType].ToString());
System.IO.MemoryStream imageMemoryStream = new System.IO.MemoryStream(imageByteArray);
try
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(imageMemoryStream))
{
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
catch (System.Exception ex)
{
Log.Error(ex, ": ConvertBytesToImage {0} ");
}
finally
{
imageMemoryStream.Close();
context.Response.Flush();
}
}