急!!!image对象存储到数据库的问题!!100分

SimerJoe 2006-07-28 02:51:48
如何将image对象行保存到数据库?
Byte[] bo = null;
MemoryStream stream = new MemoryStream();


Image image = this.pictureBox1.Image;


image.Save(stream, ImageFormat.Jpeg); //这一句报错

bo = stream.GetBuffer();


请问如何解决,谢谢
...全文
199 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jack6512006 2006-07-28
  • 打赏
  • 举报
回复
学习中
mark
SimerJoe 2006-07-28
  • 打赏
  • 举报
回复
现在最关键的是要将image 转化成为一个Byte数组
SimerJoe 2006-07-28
  • 打赏
  • 举报
回复
Image image = this.pictureBox1.Image;

由于我的image对象是经过编辑的,不是直接从文件读取的,所以不能用读取流的方式得到byte数组....

qyfjl 2006-07-28
  • 打赏
  • 举报
回复
保存:
try
{
SqlConnection conn = new SqlConnection("Server=127.0.0.1;uid=sa;pwd=123456;database=train");
string sql="select * from test1";
SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
SqlCommandBuilder MyCB = new SqlCommandBuilder(sda);
DataSet ds = new DataSet("DocFile");


string filePath=Path.GetDirectoryName(Application.ExecutablePath)+@"\File\ExamPaper.doc";
//string filePath=Path.GetDirectoryName(Application.ExecutablePath)+@"\File\1.jpg";

FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read);

byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
sda.Fill(ds,"DocFile");

DataRow myRow;
myRow=ds.Tables["DocFile"].NewRow();
myRow["doc_file"] = MyData;
ds.Tables["DocFile"].Rows.Add(myRow);
sda.Update(ds, "DocFile");

conn.Close();

MessageBox.Show ("文件保存到数据库存成功");
}
catch(Exception E)
{
MessageBox.Show(E.ToString());
}
读取:
try
{
SqlConnection conn = new SqlConnection("Server=127.0.0.1;uid=sa;pwd=123456;database=train");
string sql="select * from test1";
SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
SqlCommandBuilder MyCB = new SqlCommandBuilder(sda);
DataSet ds = new DataSet("DocFile");
sda.Fill(ds, "DocFile");
DataRow myRow=ds.Tables["DocFile"].Rows[1];
//DataRow myRow=ds.Tables["DocFile"].Rows[4];


byte[] MyData= (byte[])myRow["doc_file"];

//this.pictureBox1 .Image =Image.FromStream (new MemoryStream(MyData));

int ArraySize = new int();
ArraySize = MyData.GetUpperBound(0);
FileStream fs = new FileStream(@"C:\1.doc", FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(MyData, 0,ArraySize);
fs.Close();
MessageBox.Show ("文件从数据库中读取成功");

}
catch(Exception Ex)
{
MessageBox.Show (Ex.ToString ());
}
孟子E章 2006-07-28
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=ECD9AE16-8FF0-4A1C-9B9F-5E8B641CB1B1
孟子E章 2006-07-28
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=9154BC99-DF64-4E2D-B096-26C99CE464BE
张赐 2006-07-28
  • 打赏
  • 举报
回复
报的什么错
xlshen_lxz 2006-07-28
  • 打赏
  • 举报
回复
http://rippleyong.cnblogs.com/archive/2004/08/10/31977.aspx

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧