我的应用程序是C/S结构的,数据库中某张表有一个image字段,现在我定义了一个结构体包含了这张表的结构,譬如:
class  Car

 string  price
 int     distance
 string  name
  .....

如何在程序中建立相对于数据库中image字段的变量
  byte[] picture
可以吗?
如果是这样,我怎样将已经显示在DataGridView中的一条记录(其中包含image字段的内容)提取出来,存入我的变量中啊?
可能各位要考虑下序列化问题,因为我是要把服务器端的数据传入客户端。

解决方案 »

  1.   

    你可以写一个如下的函数来得到相应的Image
    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.   

    也可以用如下的方法,这个更安全些:
    public Image ConvertToImage(object o)
    {
    if (o is byte[])
    {
    ImageConverter imc = new ImageConverter();
    Image _img = imc.ConvertFrom(null, System.Globalization.CultureInfo.CurrentCulture, o) as Image;
    if (_img != null)
    {
    Rectangle imgRect = getImgPaintRect(rect, _img.Width, _img.Height, col.m_ValueFormat.Alignment);
    g.DrawImage(_img, imgRect);
    _img.Dispose();
    _img = null;
    return;
    }
    }
    return null;
    }
      

  3.   

    那我又该如何将一幅jpg图片通过服务器传入到数据库中呢?
    请赐教