【问题标题】:How to display binary images into a gridview in ASP.NET using C#?如何使用 C# 在 ASP.NET 中将二进制图像显示到网格视图中?
【发布时间】:2021-05-07 08:37:49
【问题描述】:

我想将二进制图像显示到名为“gvExistedCharacter”的网格视图中。我对此进行了研究,其中许多人建议使用处理程序。但是,我不知道该怎么办。

仅供参考:数据库中图像的数据类型为 image 并命名为“blueBallImage”。我还想在同一个 gridview 中显示它的 int 级别。

我试过了

    SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=MyCloudGames;Integrated Security=True");

    SqlCommand cmd = new SqlCommand("Select blueBallImage FROM CorrespondingBall", con);
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.Connection = con;

    SqlParameter ImageID = new SqlParameter("@characterID", System.Data.SqlDbType.Int);
    ImageID.Value = context.Request.QueryString["characterID"];
    cmd.Parameters.Add(ImageID);
    con.Open();
    SqlDataReader dReader = cmd.ExecuteReader();
    dReader.Read();
    context.Response.BinaryWrite((byte[])dReader["Image"]);
    dReader.Close();
    con.Close();

我收到错误“参数化查询'(@characterID int)Select blueBallImage FROM CorrespondingBall'需要参数'@characterID',但未提供。”

【问题讨论】:

    标签: c# asp.net image


    【解决方案1】:

    您可以使用处理程序在 gridview 中显示图像,您的 html 标记看起来像里面

    Gridview ItemTemplate设置图片控件src为src=~/ShowImage.ashx?id=" + id

    ShowImage.ashx 是您的处理程序,它返回 MemoryStream((byte[])img);

    在您的情况下,您的查询字符串是characterID

    所以你的图片来源是src=~/ShowImage.ashx?id=" + characterID


    更新答案:

    HTML 标记:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField HeaderText="CharacterID">
                <ItemTemplate>
                    <asp:Label ID="lblid" runat="server" Text='<%# Bind("characterID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="GameLevel">
                <ItemTemplate>
                    <asp:Label ID="lblglevel" runat="server" Text='<%# Bind("gameLevel") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Image">
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%#"ShowImage.ashx?getID="+Eval("characterID") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    背后的代码:

    protected void Page_Load(object sender, EventArgs e)
    {        
        DataTable dt = getData();
        GridView1.DataSource = dt;
        GridView1.DataBind();
      }
    
    public  DataTable getData() 
    {
      SqlDataAdapter dap = new SqlDataAdapter("select characterID,blueBallImage,gameLevel from CorrespondingBall", cn);
      DataSet ds = new DataSet();
      dap.Fill(ds);
      return ds.Tables[0];
    }
    

    通用处理程序:在此处添加新的通用处理程序 showIamges.ashx 是我的通用处理程序

    public void ProcessRequest(HttpContext context)
     {
        Int32 my_Id;
        if (context.Request.QueryString["getID"] != null)
        {
           my_Id = Convert.ToInt32(context.Request.QueryString["getID"]);
           context.Response.ContentType = "image/jpeg";
           Stream strm = ShowEmpImage(my_Id);
           byte[] buffer = new byte[4096];
           int byteSeq = strm.Read(buffer, 0, 4096);
           while (byteSeq > 0)
            {
               context.Response.OutputStream.Write(buffer, 0, byteSeq);
               byteSeq = strm.Read(buffer, 0, 4096);
            }
          }
    }
    
    public Stream ShowEmpImage(int my_Id)
    {
       string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
       SqlConnection connection = new SqlConnection(conn);
       string sql = "select blueBallImage from CorrespondingBall WHERE characterID = @ID";
       SqlCommand cmd = new SqlCommand(sql, connection);
       cmd.CommandType = CommandType.Text;
       cmd.Parameters.AddWithValue("@ID", my_Id);
       connection.Open();
       object img = cmd.ExecuteScalar();
       return new MemoryStream((byte[])img);
     }
    

    【讨论】:

    • 嗨 Satinder,我想问你一个问题。 @characterID 实际上是什么?我看到了一些教程,他们在其中放置了 @ID 等内容。请原谅我,因为我以前没有使用过处理程序。
    • id reperesnt 你想显示哪一行的图片?可能有一些关系,即像employeID,,empname,employe_image 列,所以在gridview 你绑定数据id,name,image
    • 我仍然无法得到它。如果我想显示 blueBallImage 的每张图片怎么办?
    • @LiuJiaHui:blueBallimage 是你的列名吧,那么它有很多行很多,对于每一行你需要设置各自的图像,如果你仍然有问题,请告诉我
    • blueBallImage 是我的列名,它有很多包含图像的行。我怎样才能把它全部显示出来?
    【解决方案2】:

    您想在 sql 命令中添加 @characterID 参数,但您的命令没有名为 @characterID 的参数。

    删除代码中的参数过程或在尝试匹配的 sql 命令中添加@characterID。

    【讨论】:

    • 嗨,Soner,我想显示所有 blueBallImage 图像。我可以知道我该怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    相关资源
    最近更新 更多