在数据库有个image类型的字段,我要获得这个字段的数据,并给一个Image的实例,
foreach (DataRow dr in ds.Tables["table1"].Rows)
{
    Image image = (Bitmap)dr["p_img"];
    image.save(@"D:\file1.jpg");
}
我这样写得不到图片,请问该怎么写?

解决方案 »

  1.   

    可以用如下的方法试试:
    //用(dr["p_img"] 代替 o)public Image getImage(object o)
    {
    Image img = null;
    byte[] bts = o as byte[];
    if (bts != null)
    {
    MemoryStream ms = null;
    try
    {
    ms = new MemoryStream(bts, 0, bts.Length);
    {
    img = Image.FromStream(ms, true);
    }
    }
    catch{}
    finally
    {
    if (ms != null)
    {
    ms.Close();
    }
    }
    }
    return img;
    }
      

  2.   

    foreach (DataRow dr in ds.Tables["table1"].Rows)
    {
        Image image = (Bitmap)dr["p_img"];
        image.save(@"D:\file1.jpg");
    }
    ---------------------------------
    这样写不行的问题是,你从数据库得到是的 byte[] 而不是 bitmap 所以你要这样做转换
    byte[] byt = (byte[])dr["p_img"];
    然后 才是再将你得到的 byte 数组 byt 生成一个图像
      

  3.   

    ba tupian de  2 jinzhi ma cun z  shujuku li you dian kongbu ba 
    zhi jie ba tupian shangchuan dao ni de mulu li
    zai shujuku li cun shang ta de  URL bu geng hao xie ?
      

  4.   

    to: img = Image.FromStream(ms, true);
    ===
    报错啊,参数无效
      

  5.   

    这种方法,对多 frames 的 gif 有不足的地方。  建议临时保存到磁盘,再载入。  
    img = Image.FromStream(ms, true);
      
      

  6.   

    解决了,谢谢各位,是我数据库中保存错了,存了个JPG文件的路径而不是文件本身。