2011-01-08 5 views
1

나는 System.Drawing.Image의 목록을 가지고 있습니다. 해당 목록의 마지막 이미지로 Page_Load 이벤트에서 내 asp.net Image 컨트롤을 업데이트하고 싶습니다.asp.net 이미지 컨트롤 - 이미지를 넣는 방법

ImageURL과 같은 속성 만 찾을 수 있습니다. 그냥 같은 것을 할 수 없다

ImageControl1.referenceToSomeImageObjectWhichWillBeDisplayed=myImageObjectFromTheList 

Image 컨트롤에는 이러한 속성이 있습니까?

답변

1

아니요, 없습니다.

원하는 것을하려면 이미지를 반환하는 HTTP Handler을 작성하고 ImageUrl을 가리 키십시오.

Here은 이러한 핸들러의 한 예입니다.

2

Oded 올바른 이미지를 반환하는 처리기를 사용합니다. 예 아래 :

핸들러 클래스 :

public class ImageHandler : IHttpHandler 
{ 

public void ProcessRequest(HttpContext context) 
{ 
    try 
    { 
     String Filename = context.Request.QueryString[ "FileName" ]; 

     if (!String.IsNullOrEmpty(Filename)) 
     { 
      // Read the file and convert it to Byte Array 
      string filename = context.Request.QueryString[ "FileName" ]; 
      string contenttype = "image/" + Path.GetExtension(Filename.Replace(".", "")); 
      FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); 
      BinaryReader br = new BinaryReader(fs); 
      Byte[] bytes = br.ReadBytes((Int32) fs.Length); 
      br.Close(); 
      fs.Close(); 

      //Write the file to response Stream 
      context.Response.Buffer = true; 
      context.Response.Charset = ""; 
      context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      context.Response.ContentType = contenttype; 
      context.Response.AddHeader("content-disposition", "attachment;filename=" + filename); 
      context.Response.BinaryWrite(bytes); 
      context.Response.Flush(); 
      context.Response.End(); 
     } 
    } 
    catch (Exception) 
    { 
     throw; 
    } 
} 

/// <summary> 
/// Gets whether the handler is reusable 
/// </summary> 
public bool IsReusable 
{ 
    get { return true; } 
} 
} 

나는 그 핸들러 사용하는 일반적인 페이지 방법을 추가 :

 /// <summary> 
    /// Gets the image handler query 
    /// </summary> 
    /// <param name="ImagePath">The path to the image</param> 
    /// <returns>Image Handler Query</returns> 
    protected string GetImageHandlerQuery(string ImagePath) 
    { 
     try 
     { 
      if (ImagePath != string.Empty) 
      { 
       string Query = String.Format("..\\Handlers\\ImageHandler.ashx?Filename={0}", ImagePath); 

       return Query; 
      } 
      else 
      { 
       return "../App_Themes/Dark/Images/NullImage.gif"; 
      } 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 

을 그리고 마지막으로 ASPX에서 사용 :

<asp:ImageButton ID="btnThumbnail" runat="server" CommandName="SELECT" src='<%# GetImageHandlerQuery((string)Eval("ImageThumbnail200Path")) %>' 
                      ToolTip='<%#(string) Eval("ToolTip") %>' Style="max-width: 200px; max-height: 200px" /> 

또는 코드에서 사용하려는 경우 뒤에 :

imgPicture.ImageUrl = this.GetImageHandlerQuery(this.CurrentPiecePicture.ImageOriginalPath); 

분명히 페이지 메서드가 필요 없으며 직접 처리기를 호출 할 수 있지만 기본 페이지 클래스를 넣는 것이 유용 할 수 있습니다.

관련 문제