图片文件以二进制形式存储于数据库如何读取并显示于页面?

时间:2021-01-22 21:44:03
   图片文件以二进制形式存储于数据库中得img列(不是图片路径),现在需要读取出来并显示在页面上,以下代码只能显示第一张图片,请问如何实现依次显示所有图片排列在页面上? 

  protected void Page_Load(object sender, EventArgs e)
        {
            UserBll ub = new UserBll();
            DataTable dt = ub.getImages();
            if (dt.Rows.Count > 1)
            {
                Byte[] buffer = (Byte[])dt.Rows[0]["img"];
                if (buffer.Length > 1)
                {
                    Response.BinaryWrite(buffer);
                    Response.End();
                }
            }
            this.userImages.ImageUrl = "Image.aspx";
        }

4 个解决方案

#1


插入
1:数据库里有相应的二进制字段,
2:C#里的插入语句用SqlParameter的方式将二进制(大部分是由Stream转来的)作为参数值就可以了
读取
1:一般的查询,对应的字段用byte[]接受
2:如果是网页程序只要将byte输出到Response中(从数组中一个个读取一个一个转成int后Write),如果不行将一些HTML标签删掉,C#声明的留着,如果是WinForm就 MemoryStream memoryStream = new MemoryStream(byte[])            Bitmap bitmap = new Bitmap(memoryStream) 将bitmap 给图片控件

#2


 
问题自己已经解决,谢谢!以下是我用datalist动态添加image控件分页显示图片的代码!
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserBll ub = new UserBll();
                DataTable dt = ub.getImages();
                DataView dv = dt.DefaultView;
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dv;
                pds.PageSize = 6;
                pds.AllowPaging = true;
                int totalCount = pds.PageCount;
                int currPage;
                if (Request.QueryString["Page"] != null)
                {
                    currPage = Convert.ToInt32(Request.QueryString["Page"]);
                }
                else
                {
                    currPage = 1;
                }
                pds.CurrentPageIndex = currPage - 1;
                lblCurrentPage.Text = "Page: " + currPage.ToString();
                if (!pds.IsFirstPage)
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(currPage - 1);
                 if (!pds.IsLastPage)
                 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(currPage + 1);
                //把PagedDataSource 对象赋给DataList控件
                DataList1.DataSource = pds;
                DataList1.DataBind();


            }

            
        }

#3


引用 2 楼 SmilingKevin 的回复:
 
问题自己已经解决,谢谢!以下是我用datalist动态添加image控件分页显示图片的代码!
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserBll ub = new UserBll();
                DataTable dt = ub.getImages();
                DataView dv = dt.DefaultView;
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dv;
                pds.PageSize = 6;
                pds.AllowPaging = true;
                int totalCount = pds.PageCount;
                int currPage;
                if (Request.QueryString["Page"] != null)
                {
                    currPage = Convert.ToInt32(Request.QueryString["Page"]);
                }
                else
                {
                    currPage = 1;
                }
                pds.CurrentPageIndex = currPage - 1;
                lblCurrentPage.Text = "Page: " + currPage.ToString();
                if (!pds.IsFirstPage)
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(currPage - 1);
                 if (!pds.IsLastPage)
                 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(currPage + 1);
                //把PagedDataSource 对象赋给DataList控件
                DataList1.DataSource = pds;
                DataList1.DataBind();


            }

            
        }
虽然看不懂是什么意思,还好像还是很强大的样子 图片文件以二进制形式存储于数据库如何读取并显示于页面?

#4


不是这样子的

#1


插入
1:数据库里有相应的二进制字段,
2:C#里的插入语句用SqlParameter的方式将二进制(大部分是由Stream转来的)作为参数值就可以了
读取
1:一般的查询,对应的字段用byte[]接受
2:如果是网页程序只要将byte输出到Response中(从数组中一个个读取一个一个转成int后Write),如果不行将一些HTML标签删掉,C#声明的留着,如果是WinForm就 MemoryStream memoryStream = new MemoryStream(byte[])            Bitmap bitmap = new Bitmap(memoryStream) 将bitmap 给图片控件

#2


 
问题自己已经解决,谢谢!以下是我用datalist动态添加image控件分页显示图片的代码!
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserBll ub = new UserBll();
                DataTable dt = ub.getImages();
                DataView dv = dt.DefaultView;
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dv;
                pds.PageSize = 6;
                pds.AllowPaging = true;
                int totalCount = pds.PageCount;
                int currPage;
                if (Request.QueryString["Page"] != null)
                {
                    currPage = Convert.ToInt32(Request.QueryString["Page"]);
                }
                else
                {
                    currPage = 1;
                }
                pds.CurrentPageIndex = currPage - 1;
                lblCurrentPage.Text = "Page: " + currPage.ToString();
                if (!pds.IsFirstPage)
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(currPage - 1);
                 if (!pds.IsLastPage)
                 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(currPage + 1);
                //把PagedDataSource 对象赋给DataList控件
                DataList1.DataSource = pds;
                DataList1.DataBind();


            }

            
        }

#3


引用 2 楼 SmilingKevin 的回复:
 
问题自己已经解决,谢谢!以下是我用datalist动态添加image控件分页显示图片的代码!
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserBll ub = new UserBll();
                DataTable dt = ub.getImages();
                DataView dv = dt.DefaultView;
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dv;
                pds.PageSize = 6;
                pds.AllowPaging = true;
                int totalCount = pds.PageCount;
                int currPage;
                if (Request.QueryString["Page"] != null)
                {
                    currPage = Convert.ToInt32(Request.QueryString["Page"]);
                }
                else
                {
                    currPage = 1;
                }
                pds.CurrentPageIndex = currPage - 1;
                lblCurrentPage.Text = "Page: " + currPage.ToString();
                if (!pds.IsFirstPage)
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(currPage - 1);
                 if (!pds.IsLastPage)
                 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(currPage + 1);
                //把PagedDataSource 对象赋给DataList控件
                DataList1.DataSource = pds;
                DataList1.DataBind();


            }

            
        }
虽然看不懂是什么意思,还好像还是很强大的样子 图片文件以二进制形式存储于数据库如何读取并显示于页面?

#4


不是这样子的