我开发了一个C#的WINDOWS应用,其中把图形数据保存到了一个SQL中的IMAGE字段中,但我没有办法把它读出来,具体情况如下:
1——
图形数据已成功保存到数据库中,我已通过WEB的RESPONSE.BINARYWRITE成功的显示了我所保存的图形;
2——
很多高手介绍,把image字段强制转换成byte[]就可以了,确实,我在WEB中开发的时候是成功了,但在开发WINDOWS应用的时候就总是报告说不能强行转换。
为什么?
如有需要,请参照:http://www.csdn.net/Expert/icView1.asp?id=752930
代码如下:
String constr="data source=zss;initial catalog=ZssTest;
password=norman;persist security info=True;user id=sa;
workstation id=ZSS;packet size=4096";
SqlConnection sqlcon=new SqlConnection(constr);
String sqlselect="select bin_image,id from Table_1 where id=29";
SqlCommand sqlcom=new SqlCommand(sqlselect,sqlcon);
sqlcon.Open();
SSqlDataReader reader=sqlcom.ExecuteReader();
reader.Read();
//取出数据
Byte[] bytes=(Byte[])reader["bin_image"];
//根据生成Bitmap
System.IO.MemoryStream ms=new System.IO.MemoryStream(bytes,0,bytes.
Length);
Bitmap bitmap=new Bitmap(ms,true);
//在窗口上绘制image
Graphics g=Graphics.FromHwnd(this.Handle);
g.DrawImage(bitmap,10,10,bitmap.Width,bitmap.Height);
sqlcon.Close();
this.sqlConnection1.Open();
SqlCommand sqlCommand=new SqlCommand("select id,name,age,image from students",this.sqlConnection1);
SqlDataReader sqlDataReader=sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);
sqlDataReader.Read();
textBoxId.Text=sqlDataReader.GetInt32(0).ToString();
textBoxName.Text=sqlDataReader.GetString(1);
textBoxAge.Text=sqlDataReader.GetInt32(2).ToString();
int bufferSize=100;
long alreadReadSize=0;
int readingSize;
byte[] imageFieldValue=new byte[bufferSize];
MemoryStream memoryStream=new MemoryStream();
do{
readingSize=(int)sqlDataReader.GetBytes(3,alreadReadSize,imageFieldValue,0,bufferSize);
memoryStream.Write(imageFieldValue,0,readingSize);
alreadReadSize+=readingSize;
}
while (readingSize==bufferSize);
pictureBoxImage.Image=new Bitmap(memoryStream);
sqlDataReader.Close();
this.sqlConnection1.Close();
}
绝对好,昨晚才编的?解决问题别忘了给分