2014-01-20 159 views
0

我是新來的ASP.net和有一些問題,以顯示圖像。我的設計師代碼如下。從數據庫顯示圖像ASP.Net

<div id="directory-logo-wrapper" class="floatright"> 
    <table id="wrapper1" runat="server"> 
     <tr> 
      <td> 
       <img src="../images/companylogo.png" alt="LogoWrapper" /> 
       <%--<asp:Image ID="Image1" runat="server" Visible="true" alt="LogoWrapper" />--%> 
      </td> 
     </tr> 
    </table> 
</div> 

我的代碼背後的代碼如下所示。

protected void ShowImageFile(object sender, EventArgs e) 
{ 
    byte[] bytes = {}; 
    bytes = (byte[])GetData("SELECT UploadedLogo FROM Projects WHERE ProjectId =" + id).Rows[0]["UploadedLogo"]; 
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); 
    //Image1.ImageUrl = "data:image/png;image/jpg;base64," + base64String; 
} 

private DataTable GetData(string query) 
{ 
    DataTable dt = new DataTable(); 
    string constr = ConnectionInfo.GetConnectionString(); 

    using (SqlConnection con = new SqlConnection(constr)) 
    { 
     using (SqlCommand cmd = new SqlCommand(query)) 
     { 
      using (SqlDataAdapter sda = new SqlDataAdapter()) 
      { 
       cmd.CommandType = CommandType.Text; 
       cmd.Connection = con; 
       sda.SelectCommand = cmd; 
       sda.Fill(dt); 
      } 
     } 
     return dt; 
    } 
} 

問題:如何設置table:wrapper1 img src在代碼後面,因爲它沒有在代碼後面顯示。我試圖使用Image1,但它也沒有在代碼後面訪問。

請幫幫我。

感謝,拉賈

+0

您可以使用Asp:Image with ImageUrl知道 –

回答

0

嘗試像這樣

<div id="directory-logo-wrapper" class="floatright"> 
    <table id="wrapper1" runat="server"> 
     <tr> 
      <td> 
       <img id="myimage" runat="server" src="../images/companylogo.png" /> 
      </td> 
     </tr> 
    </table> 
</div> 

您可以從代碼訪問的Src背後

嘗試這樣

    'Get byte array from image file in the database with basic query 
        SqlDataAdapter myAdapter1 = new SqlDataAdapter("Select [logo] FROM [dbo].[tblCompanyInfo]", GlobalUser.currentConnectionString); 
        DataTable dt = new DataTable(); 
        myAdapter1.Fill(dt); 

        foreach (DataRow row in dt.Rows) 
        { 
         // Get the byte array from image file 
         byte[] imgBytes = (byte[])row["logo"]; 

         // If you want convert to a bitmap file 
         TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap)); 
         Bitmap MyBitmap = (Bitmap)tc.ConvertFrom(imgBytes); 


         string imgString = Convert.ToBase64String(imgBytes); 
         // Set the source with data:image/bmp 
         myimage.Src = String.Format("data:image/Bmp;base64,{0}\"", imgString); 

        } 

編輯

012內部的GridView次

訪問圖片

private string m_ConcatUrl; 

protected void gridView_RowDataBound(Object sender, GridViewRowEventArgs args) 
{ 
    if(args.Row.RowType == DataControlRowType.DataRow) 
    { 
     Image imgCtrl = (Image) args.Row.FindControl("imgCtrl"); 
     imgCtrl.ImageUrl = m_ConcatUrl; 
    } 
} 

(或)

私人字符串m_ConcatUrl;

protected void gridView_RowDataBound(Object sender, GridViewRowEventArgs args) 
{ 
    if(args.Row.RowType == DataControlRowType.DataRow) 
    { 
     HtmlImage img = (HtmlImage) args.Row.FindControl("imgCtrl"); 
     imgCtrl.Src= m_ConcatUrl; 
    } 
} 
+0

OP明確表示圖像**存儲在數據庫中** - 不在他的文件系統中...... –

+0

@marc_s對不起。現在我改變了 –

+0

myimage不可訪問。 – Raja

0

你可以試試下面的方法:

HTML:

 <div id="directory-logo-wrapper" class="floatright"> 
     <table id="wrapper1" runat="server"> 
      <tr> 
       <td> 
        <img src='<%# ImageUrl(ProjectId) %>' alt="LogoWrapper" /> 
        <%--<asp:Image ID="Image1" runat="server" Visible="true" alt="LogoWrapper" />--%> 
       </td> 
      </tr> 
     </table> 
    </div> 

代碼背後:

private string ImageUrl(int ProjectId) 
    { 
    byte[] bytes = {}; 
     bytes = (byte[])GetData("SELECT UploadedLogo FROM Projects WHERE ProjectId =" + ProjectId).Rows[0]["UploadedLogo"]; 
     string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); 
    return "data:image/png;image/jpg;base64," + base64String; 
     //Image1.ImageUrl = "data:image/png;image/jpg;base64," + base64String; 
    } 

private DataTable GetData(string query) 
{ 
    DataTable dt = new DataTable(); 
    string constr = ConnectionInfo.GetConnectionString(); 

using (SqlConnection con = new SqlConnection(constr)) 
{ 
    using (SqlCommand cmd = new SqlCommand(query)) 
    { 
     using (SqlDataAdapter sda = new SqlDataAdapter()) 
     { 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = con; 
      sda.SelectCommand = cmd; 
      sda.Fill(dt); 
     } 
    } 
    return dt; 
} 
} 

希望這有助於。

+0

它給我錯誤頁面加載。錯誤是:CS1026:)預計 – Raja

+0

我編輯了我的答案。 :) –