首先你要将数据库里的字段设为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();你试一下,应该可以的。:)
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();你试一下,应该可以的。:)
bmp.Save(fs,System.Drawing.Imaging.ImageFormat.Bmp);
:)
我来说一句。不行啊。用到的命名空间有哪些啊,请说清楚点。
还有如何从数据库中取出Image类型的字段值,并将其值放入图片框中?
先谢了。
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;
我没时间改了,你自己看一下吧
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;
在c#中有内存流的呀,memorystream
结合大家的方法就perfect了PS:这么多人都有完整的答案,我的分怎么分阿~~
大家要小心以下情况:
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();