我的是把产品图片放入数据库管理,还是真接放在一个文件夹,然后数据库记录图片路径好,???
那么放入数据库代码要怎么写?

解决方案 »

  1.   

    将Image对象和byte[]互相转换 //需要引入的命名空间
    using System;
    using System.IO;
    using System.Drawing;
    using System.Runtime.Serialization.Formatters.Binary; //引入供序列化Image对象使用/// <summary>
    /// 将byte[]转换为Image
    /// </summary>
    /// <param name="bytes">字节数组</param>
    /// <returns>Image</returns>
    public Image ReadImage(byte[] bytes)
    {
         MemoryStream ms=new MemoryStream(bytes,0,bytes.Length);
         BinaryFormatter bf = new BinaryFormatter();
         object obj=bf.Deserialize(ms);   
      ms.Close(); 
      return (Image)obj;
    }
    /// <summary>
    /// 将Image转换为byte[]
    /// </summary>
    /// <param name="image">Image</param>
    /// <returns>byte[]</returns>
    public byte[] ConvertImage(Image image)
    {
         MemoryStream ms=new MemoryStream();
         BinaryFormatter bf = new BinaryFormatter();
         bf.Serialize(ms,(object)image); 
         ms.Close();
         return ms.ToArray();
    }选将图片序列化为byte[],存进数据库,取出来的时候反序列化,OK!!!