0
我將從數據庫中顯示圖像作爲用戶輸入圖像ID。我必須在下面的圖片框中顯示它。如何在asp.net中檢索並顯示來自數據庫的圖像
我用這個代碼base64string轉換爲圖像
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["ImageID"] != null)
{
string ImgData = Request.QueryString["ImageID"].ToString();
Byte[] bytes = Convert.FromBase64String(ImgData);
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
Response.AddHeader("content-disposition", "attachment;");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
}
和我的主網頁源代碼是
protected void Button1_Click(object sender, EventArgs e)
{
NpgsqlCommand cmd = null;
string selPhoto = @"select * from photodetails where [email protected]";
System.Drawing.Image newImage;
string [email protected]"C:\Users\Public\temp_image";
try
{
cmd = new NpgsqlCommand(selPhoto, con);
cmd.Parameters.Add("@photoid",Convert.ToInt16(txtpid.Text));
if (con.State == ConnectionState.Open)
con.Close();
con.Open();
NpgsqlDataReader drphoto = cmd.ExecuteReader();
while (drphoto.Read())
{
//System.IO.Stream fs = FileUpload1.PostedFile.InputStream;
//System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] bytes = (byte[])drphoto["photo_bytearr"]; //br.ReadBytes((Int32)fs.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
img1.ImageUrl = @"http://localhost:29450/SampleWeb/showimg.aspx?ImageData=" + Convert.ToBase64String(bytes);
}
}
catch (Exception ex)
{
}
}
,但仍然不顯示圖像?
其實,有幾十個相關的帖子。只要給網站一個搜索。 – melancia
圖像通常太大而無法保存在查詢字符串中,該查詢字符串的最大長度(取決於瀏覽器,但IE最大爲2000,FF 64K),因此您需要提出更好的設計。 –
做一個谷歌搜索「如何在ASP.NET中顯示圖片」在4000萬響應中的某個地方,我相信你會找到答案。可能在前2-3個鏈接。 – Pete