2010-02-19 65 views
2

我引用一个ASHX文件,以显示其在运行时产生的图像:显示在ASP.NET MVC运行时生成的图像:ASHX VS ImageResult

<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(); 
    } 
} 

回答

-1

ASHX比使用控制器动作更快7.23倍。

+1

你有这方面的来源吗? – 2010-02-19 09:08:26

+0

@ zaph0d这是我写的基准测试结果。 – Adeel 2010-02-19 10:05:27

-1

我认为通过ashx处理它是一种很好的方法。我在我的一些项目中使用过它,它效果很好!