//打开文件
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "图片文件(*.jpg;*.bmp;*.png;*.gif)|*.jpg;*.bmp;*.png;*.gif";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    FileInfo fileInfo = new FileInfo(dlg.FileName);
                    byte[] buffer = new byte[fileInfo.Length];
                    //using 
                    FileStream stream = fileInfo.OpenRead();                    stream.Read(buffer, 0, buffer.Length);是不是这样写不对啊?

解决方案 »

  1.   

               if (dlg.ShowDialog() == DialogResult.OK)
    {
                Stream s=  openFileDialog1.OpenFile();
                byte[] by = new byte[s.Length];
                s.Read(by, 0, by.Length);
    }
      

  2.   

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
      Stream s= openFileDialog1.OpenFile();
      byte[] by = new byte[s.Length];
      s.Read(by, 0, by.Length);
    }
      

  3.   

    FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read, FileShare.Read, 20480, true);
    BinaryReader br = new BinaryReader(fs);
    byte[] bytes = br.ReadBytes((int)fs.Length);
      

  4.   

     private SqlConnection conn=new SqlConnection();
     private SqlCommand cmd=new SqlCommand();1、插入数据库:
     //初始化Connection对象
     conn.ConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
     conn.Open();
     //初始化Command对象
     cmd.Connection=conn;
     cmd.CommandText="Insert into PICS(ID,PIC) values(1,@pic)";
     //定义二进制数组
     byte[] imagebyte=new byte[1048576];//大小为1MB
     //定义文件流
     FileStream imageStream=new FileStream(Server.MapPath("01.gif"),FileMode.Open,FileAccess.Read);
     //通过文件流读取图片
     imageStream.Read(imagebyte,0,1048576);
     //给CommondText添加属性
     cmd.Parameters.Add("@pic",SqlDbType.Image,(int)(imageStream.Length));
     cmd.Parameters["@pic"].Value=imagebyte;
     cmd.ExecuteNonQuery();
     conn.Close();
      

  5.   

    //将Image转换为byte[]
    public byte[] ConvertImage(Image image)
    {
         FileStream fs=new FileStream("imagetemp",FileMode.Create,FileAccess.Write,FileShare.None);
         BinaryFormatter bf = new BinaryFormatter();
         bf.Serialize(fs,(object)image);
         fs.Close();
         fs=new FileStream("imagetemp",FileMode.Open,FileAccess.Read,FileShare.None);
         byte[] bytes = new byte[fs.Length];
         fs.Read(bytes,0,(int)fs.Length);
         fs.Close();
         return bytes;
    }