无法使用C#添加图像

本文关键字:添加 图像 | 更新日期: 2023-09-27 18:00:16

我的代码中有一个问题。我正在尝试使用C#、Asp.net技术在GridView中添加一个图像。我只从数据库中检索图像名称,并希望将其与存储的文件夹路径一起添加。

faq.aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <table class="table table-striped table-bordered margin-top-zero" >
                    <colgroup>
                        <col class="col-md-1 col-sm-1">
                        <col class="col-md-4 col-sm-4">
                        <col class="col-md-4 col-sm-4">
                        <col class="col-md-2 col-sm-2">
                        <col class="col-md-1 col-sm-1">
                    </colgroup>
                    <thead>
                        <tr>
                            <th>Sl. No</th>
                            <th>Question</th>
                            <th>Answer</th>
                            <th>Image</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                        <td><asp:Label ID="faqid" runat="server" Text='<%#Eval("FAQ_ID") %>'></asp:Label></td>
                        <td><asp:Label ID="question" runat="server" Text='<%#Eval("Question") %>'></asp:Label></td>
                        <td><asp:Label ID="answerswer" runat="server" Text='<%#Eval("Answer") %>'></asp:Label></td>
                        <td><asp:Image ID="Image1" runat="server" border="0" name="bannerimage" style="width:70px; height:70px;" ImageUrl='C:'ASP.NET'ODIYA_Doctor_Admin'ODIYA_Doctor_Admin'Upload'<%#Eval("Image")%>' /> </td>
                        <td><a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="Edit"><i class="fa fa-edit"></i></a>
                        <a href="javascript:void(0)" data-toggle="tooltip" title="" class="btn btn-xs btn-danger" data-original-title="Delete"><i class="fa fa-times"></i></td>
                    </tr>
                </tbody>
            </table>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

faq.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    faqBL objFaqBL = new faqBL();
    GridView1.DataSource = objFaqBL.getFaqData();
    GridView1.DataBind();
}

我正在获取视图中除图像之外的所有其他数据。图像也是我表中的字段名称。请帮我解决这个问题。

无法使用C#添加图像

假设您将映像存储在SQL Server数据库中,您需要创建通用处理程序image.ashx或image.aspx,如下所示:1.创建image.aspx页面将此代码添加到Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["id"] != null)
    {
        string sql1 = "SELECT Image FROM" +
            " YourTableName WHERE faqid=" +  Convert.ToInt32(Request.QueryString["id"].ToString());
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "your connection string";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            if (dr[0] != DBNull.Value)
            {
                Byte[] bytes = (Byte[])dr[0];
            }
            else
            {
                byte[] bytes = ReadFile(Server.MapPath("~/images/empty.PNG"));
                Response.Buffer = true;
            }
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
        dr.Close();
        conn.Close();

    }
}
byte[] ReadFile(string sPath)
{
    byte[] data = null;
    FileInfo fInfo = new FileInfo(sPath);
    long numBytes = fInfo.Length;
    FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fStream);
    data = br.ReadBytes((int)numBytes);
    return data;
}
  1. 要在GridView中查看图像,请添加如下模板列:

           <asp:TemplateField HeaderText="Image">
                <ItemTemplate>
                    <div style="text-align:center;">
                    <asp:ImageButton ID="ImageButton1" runat="server" 
                    ImageUrl='<%# "image.aspx?id="+ Eval("faqid") %>' Width="100px"
                    Height="70px" Style="cursor: pointer"  />
                    </div>
                </ItemTemplate>
                <ItemStyle Width="300px" HorizontalAlign="Left" />
            </asp:TemplateField>
    

希望能有所帮助。

正如Ondrej所说,ImageUrl可与example.com/Upload/myimage.png等URL配合使用。假设Upload文件夹位于web应用程序的根目录中,则将ImageUrl更改为即可

ImageUrl='<%# "Upload/" + Eval("Image")%>'

试试看:)