2012-01-13 72 views
1

我正在將Base64代碼轉換爲圖像,我正在使用以下方式保存並顯示該圖像。在ASP.NET C中顯示圖像#

var kpin = Base64ToImage(TextBox1.Text); 
kpin.Save(@"e:\myim.png"); 
Image1.ImageUrl = @"e:\myim.png"; 

和類是

public Image Base64ToImage(string base64String) 
{ 
    byte[] imageBytes = Convert.FromBase64String(base64String); 
    MemoryStream ms = new MemoryStream(imageBytes, 0, 
     imageBytes.Length); 
    ms.Write(imageBytes, 0, imageBytes.Length); 
    Image image = Image.FromStream(ms, true); 
    return image; 
} 

這個過程工作正常,但我需要的圖像不被保存在硬盤中。如何直接顯示此圖像而不保存到硬盤並恢復。

回答

1

取而代之的是Image1.ImageURL設置爲你的圖片的路徑,可以代替做的幾件事情之一:

  1. 使用與Base64的數據直接img標籤在裏面 - http://www.sweeting.org/mark/blog/2005/07/12/base64-encoded-images-embedded-in-html 並非所有瀏覽器支持這一點。
  2. 創建一個Action或Webform(取決於您是否使用ASP.NET MVC),將輸入任何內容作爲輸入來檢索或生成Base64編碼數據,然後設置響應標頭以提供正確的內容類型(image/png或其他),並將圖像直接寫入Response.OutputStream(或在ASP.NET MVC中使用ContentResult)。通過Stackoverflow的例子噸如何做。

-M

+0

非常感謝!這對我有用! – 2012-01-13 17:51:04

0

完全不與圖像對象打擾,只是做直銷:

public void Base64ToResponse(string base64String) 
{ 
    Response.ContentType = "text/png"; //or whatever... 
    byte[] imageBytes = Convert.FromBase64String(base64String); 
    Response.OutputStream(imageBytes, 0, imageBytes.Length); 
}