Oded ist korrekt, ich würde einen Handler verwenden, um das Bild zurückzugeben. Beispiel unten:
Handler Klasse:
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; }
}
}
Ich habe dann eine gemeinsame Seite Methode den Handler zu verwenden:
/// <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;
}
}
Und schließlich in der ASPX zu verwenden:
<asp:ImageButton ID="btnThumbnail" runat="server" CommandName="SELECT" src='<%# GetImageHandlerQuery((string)Eval("ImageThumbnail200Path")) %>'
ToolTip='<%#(string) Eval("ToolTip") %>' Style="max-width: 200px; max-height: 200px" />
oder wenn Sie in Code hinter verwenden wollten:
Offensichtlich brauchen Sie nicht die Seitenmethode und Sie können den Handler direkt aufrufen, aber es könnte nützlich sein, Ihre Basisseitenklasse einzutragen.