日期:2014-05-18  浏览次数:20913 次

从数据库sql server 读取图片A generic error occurred in GDI+.错误
private void button1_Click(object sender, EventArgs e)
  {
  SqlConnection conn = new SqlConnection(@"Data source=HERO;Integrated Security = SSPI;Initial Catalog = DevAndInnov");
  conn.Open();
  SqlCommand cmd = new SqlCommand("select im from test where xh='1a11'", conn);
  byte[] b = (byte[])cmd.ExecuteScalar();
  if (b.Length > 0)
  {
  MemoryStream stream = new MemoryStream(b, true);
  stream.Write(b, 0, b.Length);
  pictureBox1.Image = new Bitmap(stream);//该行出错*************
  stream.Close();

  }  
  }

请高手指教数据库im字段类型image

------解决方案--------------------
你为什么还要
ms.Write(b, 0, b.Length); ?
直接
byte[] b = (byte[])cmd.ExecuteScalar(); 
if (b.Length > 0) 

MemoryStream stream = new MemoryStream(b, true); 
pictureBox1.Image = new Bitmap(stream);
stream.Close(); 

}

不就可以了
------解决方案--------------------
C# code

SqlConnection conn = new SqlConnection("server=.;database=dbImate;uid=sa;pwd=;");
            conn.Open();

            SqlCommand cmd = new SqlCommand("select * from [image]", conn);
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();

            MemoryStream ms = new MemoryStream((Byte[])dr["pic"]);
            Bitmap image = new Bitmap(ms);
            image.Save(Server.MapPath("~/1.jpg"));

            this.Image1.ImageUrl = "1.jpg";

            dr.Close();
            conn.Close();

------解决方案--------------------
C# code
byte[] b = (byte[])cmd.ExecuteScalar(); 
            if (b.Length > 0) 
            { 
                MemoryStream stream = new MemoryStream(b, true); 
                stream.Position = 0;//加上这句看看
                pictureBox1.Image = new Bitmap(stream); 
                stream.Close(); 

            }