首先你要将数据库里的字段设为image类型
Bitmap bmp = this.pictureBox1.Image;
Stream fs;
bmp.Save(fs,ImageFormat.Bitmap);
byte[] Data = new byte[fs.Length];
fs.Read(Data,0,Convert.ToInt32(fs.Length));
dr["DocPhoto"] = Data;
fs.Close();你试一下,应该可以的。:)

解决方案 »

  1.   

    不好意思,我想起来了,bmp.Save(fs,ImageFormat.Bitmap);应该是
    bmp.Save(fs,System.Drawing.Imaging.ImageFormat.Bmp);
      

  2.   

    嗯,可能需要:Stream fs = Stream.Null;
    :)
      

  3.   

    TO:klxyz(小康)
    我来说一句。不行啊。用到的命名空间有哪些啊,请说清楚点。
    还有如何从数据库中取出Image类型的字段值,并将其值放入图片框中?
    先谢了。
      

  4.   

    byte[] MyData = new byte[0];
    MyData = (byte[])dr["DocPhoto"];
    int BmpSize = new int();
    用到的名称空间:System.Drawing;System.IO;我用的是文件流BmpSize = MyData.Length;
    string CurPath = "C:\temp.bmp";
    if(File.Exists(CurPath))
    File.Delete(CurPath);
    FileStream fs =new FileStream(CurPath,FileMode.OpenOrCreate,FileAccess.Write);
    fs.Write(MyData,0,BmpSize);
    fs.Close();
    bmp =new Bitmap(CurPath);
    this.pictureBox1.Image = bmp;
    我没时间改了,你自己看一下吧
      

  5.   

    Stream imgdatastream = File1.PostedFile.InputStream;int imgdatalen = File1.PostedFile.ContentLength;string imgtype = File1.PostedFile.ContentType;string imgtitle = TextBox1.Text;byte[] imgdata = new byte[imgdatalen];int n = imgdatastream.Read(imgdata,0,imgdatalen);string connstr=((NameValueCollection)Context.GetConfig("appSettings"))["connstr"];SqlConnection connection = new SqlConnection(connstr);SqlCommand command = new SqlCommand("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)VALUES ( @imgtitle, @imgtype,@imgdata )", connection ); SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 );paramTitle.Value = imgtitle;command.Parameters.Add( paramTitle); SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );paramData.Value = imgdata;command.Parameters.Add( paramData ); SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 );paramType.Value = imgtype;command.Parameters.Add( paramType ); connection.Open();int numRowsAffected = command.ExecuteNonQuery();connection.Close();
      

  6.   

    byte[] MyData = new byte[0];
    MyData = (byte[])dr["DocPhoto"];
    int BmpSize = new int();
    用到的名称空间:System.Drawing;System.IO;我用的是文件流BmpSize = MyData.Length;
    string CurPath = "C:\temp.bmp";
    if(File.Exists(CurPath))
    File.Delete(CurPath);
    FileStream fs =new FileStream(CurPath,FileMode.OpenOrCreate,FileAccess.Write);
    fs.Write(MyData,0,BmpSize);
    fs.Close();
    bmp =new Bitmap(CurPath);
    this.pictureBox1.Image = bmp;
      

  7.   

    嘻嘻,为什么大家都用文件流的阿?
    在c#中有内存流的呀,memorystream
    结合大家的方法就perfect了PS:这么多人都有完整的答案,我的分怎么分阿~~
      

  8.   

    天啊~~~~~~~我一个晚上的时间就这样没了!!!!
    大家要小心以下情况:
    Image bmp = this.pictureBox1.Image;
    MemoryStream fs = new MemoryStream() ;
    bmp.Save(fs,ImageFormat.Bmp);
    byte[] Data = new byte[fs.Length];
    fs.position = 0;
    ~~~~~~~~~~~~~~~
    就是这句,这句浪费了我一个晚上!!!
    可恶的C#,调用bmp.save(fs....)那个函数之后
    指针居然变成了中间一个随机数。。
    呜呜,大家小心小心啊!!!
    fs.Read(Data,0,Convert.ToInt32(fs.Length));
    dr["DocPhoto"] = Data;
    fs.Close();