バイト配列をBase64イメージに変換できます。
public string getBase64Image(byte[] myImage)
{
if (myImage!= null)
{
return "data:image/jpeg;base64," + Convert.ToBase64String(myImage);
}
else
{
return string.Empty;
}
}
あなたのイメージタグは次のようになります。
以上の画像(および他のファイルタイプ)、それは次のようになりますGeneric Handler
public void ProcessRequest(HttpContext context)
{
//check if the querystring 'id' exists
if (context.Request.QueryString["id"] != null)
{
string idnr = context.Request.QueryString["id"].ToString();
//check if the id falls withing length parameters
if (idnr.Length > 0 && idnr.Length < 40)
{
//get the data from the db or other source
byte[] bin = getMyDataFromDB();
//clear the headers
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.Clear();
context.Response.Buffer = true;
//if you do not want the images to be cached by the browser remove these 3 lines
context.Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetValidUntilExpires(false);
//set the content type and headers
context.Response.ContentType = "image/jpeg";
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"myImage.jpg\"");
context.Response.AddHeader("content-Length", bin.Length.ToString());
//write the byte array
context.Response.OutputStream.Write(bin, 0, bin.Length);
//cleanup
context.Response.Flush();
context.Response.Close();
context.Response.End();
}
}
}
あなたのイメージタグを使用することをお勧めしますため<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgA...">
:<img src="/Handler1.ashx?id=AB-1234">
実際には、イメージ自体をsrcタグに埋め込むことはできませんでした。 私はこれをすぐに試してみます。 – rxj
Base64に変換すると大きな文字列が作成されることに注意してください。私は通常、50 kb以上の画像で最初の方法を使用しません。 – VDWWD
@VDWWD ashxハンドラを作成する必要はありません。コード提供画像をbyte [](2番目の解決策)として、通常のgetimage.aspxページのPage_Load()で動作させます。単に 'context.'を' thisに置き換えます。 '(または何も) –