魔灵雪010
2021-04-21 17:20
采纳率: 100%
浏览 20

Base64在ASP.NET中怎么放到Image控件上啊,求大神指点下

我做了一个图片转码入库,但是入库出来之后怎么把它解码放Image控件上,我能想到的方法就是解码到本地路径,然后放路径,但是可以实现不用解到本地,直接解码放到Image控件上啊求指点

页面代码如下:

    <form id="form1" runat="server">
        <div id="content">
            <asp:FileUpload ID="Fu_Demo1" runat="server" />
            <asp:TextBox ID="txt_imgname" runat="server" />
            <asp:Button ID="Btn_Demo1" Text="获取" runat="server" OnClick="Btn_Demo1_Click" />
            <asp:Repeater ID="rp_imagesShow" runat="server">
                <ItemTemplate>
                    <div class="show">
                        <a href="#"><h3><%# Eval("Name") %></h3></a>
                        <asp:Image CssClass="imagesItem" ImageUrl="<%# Eval("Img") %>"" runat="server" />
                    </div>
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </form>

我的转码代码如下:可以转成功,也可入库

            if (string.IsNullOrEmpty(txt_imgname.Text))
            {
                Response.Write("<script>alert('请输入图片名!')</script>");
                return;
            }
            string imgname = txt_imgname.Text.Trim();
            string filename = Fu_Demo1.FileName;
            string fix = filename.Substring(filename.LastIndexOf('.')+1).ToLower();
            filename = Guid.NewGuid().ToString() + "." + fix;
            string newpath = "MyFile/" + filename;
            Fu_Demo1.SaveAs(Server.MapPath(newpath));

            newpath = Server.MapPath(".")+"\\"+newpath;

            if (File.Exists(newpath))
            {
                var byteArray = File.ReadAllBytes(newpath);
                string base64str = Convert.ToBase64String(byteArray);
                //Response.Write($"<script>alert('{base64str}')</script>");
                string sql = $"insert ImagesToBase values('{imgname}','{base64str}')";
                SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=ImageToBase;Integrated Security=True");
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql,conn);
                int result= cmd.ExecuteNonQuery();
                conn.Close();
                if (result==1)
                {
                    File.Delete(newpath);
                    if (!File.Exists(newpath))
                    {
                        Response.Write("<script>alert('新增成功!')</script>");
                        return;
                    }
                }
                Response.Write("<script>alert('新增失败!')</script>");
                return;
            }
            Response.Write($"<script>alert('未找到文件!')</script>");

转码就是这样子了;

解码,我定义了个实体类对应数据库:

    public class Images
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Img { get; set; }
    }

查询方法:

        private DataTable GetAllImages()
        {
            string sql = $"select * from ImagesToBase";
            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=ImageToBase;Integrated Security=True");
            conn.Open();
            SqlDataAdapter dap = new SqlDataAdapter(sql,conn);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            DataTable tab = ds.Tables[0];
            conn.Close();
            return tab;
        }

解码这里,不会搞了,没头苍蝇乱撞,毫无办法:

            List<Images> imgmods = new List<Images>();
            DataTable dt = GetAllImages();
            foreach (DataRow item in dt.Rows)
            {
                Images imgmod = new Images();
                imgmod.Id = Convert.ToInt32(item["id"]);
                imgmod.Name = item["name"].ToString();


                string imgbackstr = item["img"].ToString();
                var bt = Convert.FromBase64String(imgbackstr);

                // 搞到这步咱不会搞了,求大佬解惑啊,如果我的思路不对,还请指导指导。
                imgmods.Add(imgmod);
            }
            rp_imagesShow.DataSource = imgmods;
            rp_imagesShow.DataBind();

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 封印di恶魔 2021-04-21 17:44
    已采纳

    base64可以直接用img标签显示,要拼上前缀   

    src="data:image/jpeg;base64,(base64字符串)"

    点赞 评论
  • 一路向北999 2021-04-21 18:23

    先把base64转成byte[] 然后byte[]再转成image

    点赞 评论

相关推荐 更多相似问题