我是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,但它也无法访问后面的代码。
请帮帮我。
谢谢,Raja
答案 0 :(得分: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>
您可以从Code Behind
访问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);
}
修改强>
访问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;
}
}
答案 1 :(得分: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;
}
}
希望这有帮助。