2010-02-19 45 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處理它是一種很好的方法。我在我的一些項目中使用過它,它效果很好!