我想要一段 关于图片的存储到数据库中并且读取出来的代码 越详细越好 曾经在网上找过 但是 不是我要的 我要的是 比如 录入人事档案 要有其照片的操作 谢谢各位大侠了 谢谢 (是在winform中的 ) 答案好的 可以追加分数 谢谢帮忙啊 

解决方案 »

  1.   

     Stream ms;
                byte[] picbyte;
                OpenFileDialog ofdSelectPic = new OpenFileDialog();
                if (ofdSelectPic.ShowDialog() == DialogResult.OK)
                {
                    if ((ms = ofdSelectPic.OpenFile()) != null)
                    {
                        picbyte = new byte[ms.Length];
                        ms.Position = 0;
                        ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
                          SqlConnection conn = new SqlConnection();
                        conn.ConnectionString = "";                    sql = "Insert into Person(Photo) values(@Image)";
                        SqlCommand cmd = new SqlCommand(sql, conn);                    cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
                        cmd.Parameters["@Image"].Value = picbyte;                    conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();                    ms.Close();
                    }
                } SqlConnection conn=new SqlConnection(); 
     conn.ConnectionString=""; 
     string strSql=""; 
     SqlCommand cmd=new SqlCommand(strSql,conn); 
     conn.Open(); 
     SqlDataReader reader=cmd.ExecuteReader(); 
      reader.Read(); 
     MemoryStream ms=new MemoryStream((byte[])reader["Photo"]); 
     Image image=Image.FromStream(ms,true); 
     reader.Close(); 
     conn.Close(); 
     picturebox1.Image=image; 
      

  2.   

    FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
    long len=mystream.Length;
    mycmd.Parameters.Add("@image",SqlDbType.Image,(int)len,"picture");
    mycmd.Parameters["@image"].Direction=System.Data.ParameterDirection.Input;
    byte []box=new byte[len]; 
    mystream.Read(box,0,(int)len);
    mycmd.Parameters["@image"].Value=box;
      

  3.   

    图片存入到数据库关键是把图片序列化和反序列化下面我贴一个封装了序列化和反序列化图片的SmartImage类
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Drawing;namespace Ft.SmartImage
    {    /// <summary>
        /// 图片格式枚举类
        /// </summary>
        public enum ImageFormat
        {
            UnKown,
            Bmp,
            Jpeg,
            Png
        }    public class SmartImage
        {        #region 构造函数        private string _filePath = string.Empty;
            ImageFormat _imgFormat = ImageFormat.UnKown;        /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="filePath"></param>
            public SmartImage(string filePath)
            {
                _filePath = filePath;            if (isExist())
                {
                    _imgFormat = GetFormate(filePath);
                }
                else
                {
                    throw new Exception("文件不存在");
                }
            }        #endregion        #region 序列号二进制流        /// <summary>
            /// 图片二进制流
            /// </summary>
            public byte[] ImageStream
            {
                get
                {                Image img = null;                //读入图片
                    if (_imgFormat == ImageFormat.UnKown)
                    {
                        img = ImageResource.question;
                    }
                    else
                    {
                        img = Image.FromFile(_filePath);
                    }
                    return ToByte(img);
                }
            }        /// <summary>
            /// 得到压缩版的二进制流
            /// </summary>
            public byte[] ReduceImageStream
            {
                get
                {                Image img = null;                //读入图片
                    if (_imgFormat == ImageFormat.UnKown)
                    {
                        img = ImageResource.question;
                    }
                    else
                    {
                        img = Image.FromFile(_filePath);
                    }                img = ReduceImage(img);                return ToByte(img);
                }
            }        /// <summary>
            /// 图片格式
            /// </summary>
            public string Format
            {
                get
                {
                    string s_result = "UnKown";                switch (_imgFormat)
                    {
                        case ImageFormat .Bmp :
                            s_result = "BMP";
                            break;
                        case ImageFormat .Jpeg :
                            s_result = "JPEG";
                            break;
                        case ImageFormat .Png :
                            s_result = "PNG";
                            break;                    default :
                            s_result = "UnKown";
                            break;
                    }                return s_result;
                }
            }        /// <summary>
            /// 检查文件是否存在
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <returns>是否存在</returns>
            private bool isExist()
            {
                bool b_result = false;            b_result = File.Exists(_filePath);            return b_result;
            }        /// <summary>
            /// 获取文件的格式
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <returns>文件格式</returns>
            private ImageFormat GetFormate(string filePath)
            {
                ImageFormat img_result = ImageFormat.UnKown;            FileInfo flInfo = new FileInfo(filePath);            string s_format = flInfo.Extension;
                s_format = s_format.ToUpper();            switch (s_format )
                {
                    case ".BMP":
                        img_result = ImageFormat.Bmp;
                        break;
                    case ".JPG":
                        img_result = ImageFormat.Jpeg;
                        break;
                    case ".PNG":
                        img_result = ImageFormat.Png;
                        break;                default :
                        img_result = ImageFormat.UnKown;
                        break;
                }            return img_result;        }        /// <summary>
            /// 得到二进制流
            /// </summary>
            /// <returns></returns>
            private Byte[] ToByte(Image img)
            {            byte[] bt_result;
                MemoryStream ms = new MemoryStream();            switch (_imgFormat)
                {
                    case ImageFormat.UnKown :
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                    case ImageFormat .Bmp :
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                        break;                case ImageFormat .Jpeg :
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                    case ImageFormat .Png :
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        break;
                }            bt_result = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(bt_result, 0, System.Convert.ToInt32(ms.Length));            return bt_result;        }        #endregion        #region  静态方法        /// <summary>
            /// 得到JPG格式的图片
            /// </summary>
            /// <param name="data">二进制流</param>
            /// <returns>图片</returns>
            public static Image ToImage(byte[] data)
            {
                Image img = null;
                if (data != null)
                {
                    MemoryStream ms = new MemoryStream();                ms.Write(data, 0, data.Length);                img = Image.FromStream(ms);
                }
                else
                {
                    img = DefaultImage;
                }            return img;
            }        public static Image DefaultImage
            {
                get
                {
                    return ImageResource.question;
                }
            }        public static byte[] GetByte(Image img)
            {
                byte[] bt_result;
                MemoryStream ms = new MemoryStream();
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);            bt_result = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(bt_result, 0, System.Convert.ToInt32(ms.Length));            return bt_result;
            }        public static byte[] DefaultImageByte
            {
                get
                {
                    Image img = null;
                    //读入图片
                    img = ImageResource.question;                byte[] bt_result;
                    MemoryStream ms = new MemoryStream();
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);                bt_result = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(bt_result, 0, System.Convert.ToInt32(ms.Length));                return bt_result;
                }
            }        #endregion        #region 压缩图片        public Image ReduceImage(Image imgOriginality)
            {
                Image imgReduce = imgOriginality;
                Bitmap bmp = null;
                Graphics grap = null;            int i_witdh = imgOriginality.Width;
                int i_height = imgOriginality.Height;            bmp = new Bitmap(i_witdh, i_height);
                grap = Graphics.FromImage(bmp);
                grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                grap.DrawImage(imgReduce, new Rectangle(0, 0, i_witdh, i_height));            return imgReduce;        }        #endregion    }
    }