happyleo12
2017-04-10 08:53
采纳率: 80.8%
浏览 1.6k
已采纳

如何把二进制图片读取到image控件

大神帮帮忙看看

 //SQLHelper.cs
 //从数据库中读取image类型图片并且显示到控件上
        public bool ShowImage(string sql, System.Web.UI.WebControls.Image img1)
        {
            SqlConnection imgConn = new SqlConnection(connectionString);
            try
            {
                SqlCommand cmd = new SqlCommand(sql, imgConn);
                imgConn.Open();
                byte[] b = (byte[])cmd.ExecuteScalar();
                if (b.Length > 0)
                {
                    MemoryStream stream = new MemoryStream(b, true);
                    stream.Write(b, 0, b.Length);
                    Image img = Image.FromStream(stream);
                    //img.ImageUrl = new System.Drawing.Bitmap(stream);
                    stream.Close();
                }
                return true;
            }
            catch
            {
                img1 = null;
                return false;
            }
            finally
            {
                imgConn.Close();
            }
        }
//后台
//显示图片
 sp.ShowImage("Select Photo From Description1 Where id='" +this.id.Text + "'", imgphoto);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题