1.数据库中某个字段存有图片的本地地址,怎么根据这个地址在form里显示图片?2.如果这个sql server数据库的字段存的不是本地地址,而是image类型的话,怎么在form里显示这个图片?急,感谢各位了

解决方案 »

  1.   

    数据库存图片一般采取两种方式;
    1、存储图片地址。
    2、存储图片本身(用image字段)对于前者,与本地没有任何区别,你只要读出地址字符串即可。
    对于后者,由于它直接存储的是二进制数据,你需要还原成Image对象。
      

  2.   

    MemoryStream stream = new MemoryStream (); 
    //数据库
    byte[] image = (byte[]) command.ExecuteScalar ();
    //把文件读成byte[]就不用说了吧
    byte[] image = 从文件读取
    stream.Write (image, 0, image.Length);
    Bitmap bitmap = new Bitmap (stream); 
    弄成了Bitmap ,你就可以显示了
    或者你还可以弄成Image
      

  3.   

    1、如果是地址的话得到这个地址后pictureBox.ImageLocation=地址就可以了
    2、但如果是image类型用lovefootball
    byte[]===>MemoryStream===>Image
      

  4.   

    1、如果是地址,得到这个地址后将pictureBox.ImageLocation=地址;
    2、如果是image类型用
    private void GetImage()
    {
    if (this.pictureBox1.Image!=null)
    {
    this.pictureBox1.Image.Dispose();
    }
    int num=this.dataGrid1.CurrentRowIndex;
    if (num==-1)
    {
    return;
    }
    try
    {
    if(ds.Tables["table"].Rows[num]["Photo"]!=Convert.DBNull)
    {
    byte[] bytes=(byte[])ds.Tables["table"].Rows[num]["Photo"];
    MemoryStream memStream=new MemoryStream(bytes);
    Bitmap bitmap=new Bitmap(memStream);
    memStream.Close();
    this.pictureBox1.Image=bitmap;
    }
    else
    {
    this.pictureBox1.Image=null;
    }
    }
    catch(Exception err)
    {
    MessageBox.Show(err.Message);
    }
    }