2013-01-02 68 views

回答

1

類假設你有一個字節數組:

byte[] imageBackground = new byte[] { 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,... }; 

您可以創建一個Image

using (var stream = new MemoryStream(imageBackground)) 
using (var image = Image.FromStream(stream)) 
{ 
    // do something with the image 
} 

,或者如果你想顯示它的一個ASP.NET Web窗體上,一個Image控制裏面,你可以編寫一個通用的處理程序,將這個圖像流的響應:

public class ImageHandler : IHttpHandler 
{ 
    public bool IsReusable 
    { 
     get { return true; } 
    } 

    public void ProcessRequest(HttpContext context) 
    { 
     var response = context.Response; 
     byte[] imageBackground = new byte[] { 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,... }; 
     response.OutputStream.Write(imageBackground, 0, imageBackground.Length); 
     response.ContentType = "image/jpeg"; 
    } 
} 

然後點圖片控制到這個通用處理程序:

<asp:Image runat="server" ID="myimage" ImageUrl="~/imagehandler.ashx" />