我有System.Drawing.Image
的列表。我想在Image
事件中使用该列表中的最后一个图像更新我的asp.net Page_Load
控件。
我只能找到像ImageURL这样的属性。我不能只做像
这样的事情ImageControl1.referenceToSomeImageObjectWhichWillBeDisplayed=myImageObjectFromTheList
Image控件上有这样的属性吗?
答案 0 :(得分:2)
Oded是正确的我将使用处理程序返回Image。示例如下:
处理程序类:
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; }
}
}
然后我添加了一个常用的Page方法来使用处理程序:
/// <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" />
或者如果你想在Code Behind中使用:
imgPicture.ImageUrl = this.GetImageHandlerQuery( this.CurrentPiecePicture.ImageOriginalPath );
显然你不需要页面方法,你可以直接调用处理程序,但放入基页类可能很有用。
答案 1 :(得分:1)