2009-11-10 29 views
0

我仍然患有從SQL數據庫中檢索圖像的麻煩。這是我從數據庫插入和檢索圖像的最終解決方案。這裏是我的所有代碼:
GUI部分:請修改此代碼以從sqldatabase檢索圖像

<div> 
    <asp:Label ID="lblImage" runat="server" Text="Image"></asp:Label> 
    &nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:FileUpload ID="imageUpload" runat="server" /> 
    <br /> 
    <asp:Label ID="lblFilename" runat="server" Text="Filename"></asp:Label> 
    <asp:TextBox ID="txtFilename" runat="server"></asp:TextBox> 
    <br /> 
    <br /> 
    <asp:Button ID="BtnSave" runat="server" Text="SAVE" onclick="BtnSave_Click" /> 
    <br /> 
    <br /> 
    <br /> 
    <asp:Image ID="Image1" runat="server" /> 
</div> 

和按鈕單擊事件下我寫了下面的代碼:

protected void BtnSave_Click(object sender, EventArgs e) 
{ 
    string uploadFileName = string.Empty; 
    byte[] imageBytes = null; 
    if (imageUpload != null && imageUpload.HasFile) 
    { 
     uploadFileName = imageUpload.FileName; 
     imageBytes = imageUpload.FileBytes; 
    } 
    string str = ConfigurationManager.ConnectionStrings["ImageConnectionString"].ConnectionString; 
    SqlConnection con = new SqlConnection(str); 
    SqlCommand com = new SqlCommand("INSERT INTO REPORT_TABLE (IMAGEFIELD,IMAGENAME) VALUES (@image,@filename)", con); 
    com.Parameters.Add("@image", SqlDbType.Image, imageBytes.Length).Value = imageBytes; 
    com.Parameters.Add("@filename", SqlDbType.VarChar, 50).Value = uploadFileName; 
    con.Open(); 
    com.ExecuteNonQuery(); 
    con.Close(); 

    SqlConnection conn = new SqlConnection(str); 
    string sql = "SELECT * FROM [REPORT_TABLE]";  
    SqlDataAdapter da = new SqlDataAdapter(sql, conn); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    DataRow dr = ds.Tables[0].Rows[0]; 
    Byte[] b = (Byte[])dr["IMAGEFIELD"]; 
    MemoryStream ms = new MemoryStream(b);  

**this.pictureBox1.Image = Image.FromStream(ms)**; //(This code is for a Windows application but I want to retrieve an image from a web application) so what should be written instead of the highlighted code? In imagecontrol, image id is Image1. 
} 
+0

反正我有更新你的問題: ):) – 2009-11-10 06:52:40

回答

0

你可以這樣服務器映像:

..... 
    Image img= Image.FromStream(ms); 
    Response.Clear(); 
    Response.ContentType = "image/jpeg"; 
    img.Save(Response.OutputStream, ImageFormat.Jpeg); 
} 

但這不會把圖像放在<asp:Image ID="Image1" runat="server" />,因爲它需要圖像url不是圖像對象:(

你可以做的是設置一個單獨的頁面來提供圖像,並將圖像ID或與圖像相關的其他唯一標識符傳遞給<asp:Image ID="Image1" runat="server" />。只需添加新的頁面到您的解決方案說ImageServer.aspxpage_load寫入以下內容:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(Request.QueryString.HasValues()) 
    { 
      var id=Request.QueryString["id"]; 
      if(!string.IsEmptyOrNull(id)) 
      { 
       SqlConnection conn = new SqlConnection(str); 
       //CHANGE SELECT TO GET ONLY IMAGE WITH PASSED ID 
       string sql = "SELECT * FROM [REPORT_TABLE]"; 
       SqlDataAdapter da = new SqlDataAdapter(sql, conn); 
       DataSet ds = new DataSet(); 
       da.Fill(ds); 
       DataRow dr = ds.Tables[0].Rows[0]; 
       Byte[] b = (Byte[])dr["IMAGEFIELD"]; 
       MemoryStream ms = new MemoryStream(b);  

       Response.Clear(); 
       Response.ContentType = "image/jpeg"; 
       var img=system.Drawing.Image.FromStream(ms); 
       img.Save((Response.OutputStream, ImageFormat.Jpeg); 
       Response.Flush(); 
       return; 
      } 
      //HERE YOU MAY RETURN DEFAULT OR ERROR IMAGE 
    } 
} 

現在改變你按一下按鈕在上傳頁面如下:

protected void BtnSave_Click(object sender, EventArgs e) 
{ 
    .... 
    //SAVE IMAGE TO DB AND GET IMAGE ID (TO IDENTIFY THIS IMAGE) 
    image.ImageUrl = "YOUR_SERVER\ImageServer.aspx?id=" + IMAGE_ID; 
} 
相關問題