我在做一个winform项目时,要把一个图片存入SQL 中,然后再读入图片控件中,我的存入代码是:
byte [] imageData;
foreach(string filePath in open.FileNames)
{
FileInfo fi=new FileInfo(filePath);
imageData=new byte[fi.Length];
FileStream fs=fi.OpenRead();
fs.Read(imageData,0,ConvertInt32(fi.Length));
}
然后我执行了一个SQl语句,insert into aa values('123',imageData) ,其中123为图片编号,imageData在SQL中是image类型,,, 执行后操作成功 现在问题是,我想把图片从SQl中读出来,又写了以下代码,,可读出来却不行,读出代码如下:
object Tempob=CommonA.ExecuteScalar("select imageData from aa where id='123'");// 本人省去SQL操作的一段
byte[] imgs=(byte[])Tempob;
MemoryStream ms=new MemoryStream(imgs);
picMyPic.Image=Image.FromStream(ms);执行后没有任何语法错误,但就是读不出图片来,也没法在 picMyPic 图片控件中显示出来有谁知道是什么原因啊?? 我测试过了,SQL中是写入成功的,好像读出来后的文件是不完整的(我的猜测),
byte [] imageData;
foreach(string filePath in open.FileNames)
{
FileInfo fi=new FileInfo(filePath);
imageData=new byte[fi.Length];
FileStream fs=fi.OpenRead();
fs.Read(imageData,0,ConvertInt32(fi.Length));
}
然后我执行了一个SQl语句,insert into aa values('123',imageData) ,其中123为图片编号,imageData在SQL中是image类型,,, 执行后操作成功 现在问题是,我想把图片从SQl中读出来,又写了以下代码,,可读出来却不行,读出代码如下:
object Tempob=CommonA.ExecuteScalar("select imageData from aa where id='123'");// 本人省去SQL操作的一段
byte[] imgs=(byte[])Tempob;
MemoryStream ms=new MemoryStream(imgs);
picMyPic.Image=Image.FromStream(ms);执行后没有任何语法错误,但就是读不出图片来,也没法在 picMyPic 图片控件中显示出来有谁知道是什么原因啊?? 我测试过了,SQL中是写入成功的,好像读出来后的文件是不完整的(我的猜测),
解决方案 »
- 如何将两个下拉列表框进行相对绑定
- Word编程,如何清空Document添加的Range对象。
- c#循环生成按钮的问题
- 关于gridview更新问题
- 需要一个正则表达式,谢谢
- 关于GZipStream压缩输出流的问题?
- C# Exe文件检查有无证书?
- [在线等待、给分]怎么取数据库中的字符串的前10个字符?
- 用c#.asp.net和vc.net编程需要装哪些东东?是不是与vc++6.0一样一次就可以搞定???是不是非得装xp系统,2000行不行??
- 用代码生成的lable,如何给她注册事件?
- 前几天还能用的代码,这几天出现如下现象,是不是中毒了,如果是的话还能修复吗?怎么修复,请高手指点,谢谢!
- 自己封装的TcpListener类,当客户端主动断开时,出错,请帮忙指正!!!
of.ShowDialog (); //打开文件
string filename= of.FileName; //获取文件路径
FileStream fs = new FileStream(filename ,FileMode .Open ); //创建文件流对象
byte []bytes=new byte [fs.Length ]; //创建字节数组
fs.Read(bytes ,0,(int )fs .Length ); //打开Read方法
string sql = "insert into stu values(@images)"; //编写sql语句
SqlCommand cmd = new SqlCommand(sql,DB .con ); //创建SqlCommand对象
DB.con.Open(); //打开数据库
cmd.Parameters.Add("@images", SqlDbType.Image).Value = bytes; //为参数赋值
cmd.ExecuteNonQuery(); //执行sql命令
DB.con.Close(); //关闭数据库 //从数据库中提取图像string sql = "select top 1 * from stu order by id desc "; //编写sql语句
SqlCommand cmd = new SqlCommand(sql,DB .con ); //创建SqlCommand对象 DB.con.Open(); //打开数据库
SqlDataReader read = cmd.ExecuteReader(); //创建SqlDataReader对象
read.Read(); //打开读取的方法
byte []bytes=(byte[])read ["image"]; //读取
read.Close(); //关闭读取的方法
DB.con.Close(); //关闭数据库
MemoryStream m = new MemoryStream(bytes ); //创建支持读取区的内存流
pictureBox1.Image = Image.FromStream(m); //显示在图片框上
FileStream fs=fi.OpenRead();
fs.Seek(0, SeekOrigin.Begin);
fs.Read(imageData,0,ConvertInt32(fi.Length));你再检查下,读出来的byte有值吗?
http://topic.csdn.net/u/20100310/17/c4bbf36c-ff98-4472-aeec-3618fac448d3.html
http://blog.163.com/tuibianzhilv001/blog/static/167563877201081013131221/
http://www.7747.net/kf/201007/52486.html
参考
是不是存的时候出的问题,换一种方式试试
直接FileStream fs = new FileStream(filename ,FileMode.Open )试试
数据库结构
create table test
{
id identity(1,1),
FImage image
}
相关的存储过程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO 在UpPhoto.aspx文件中添加如下:
<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传"></asp:Button> 然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码:
private void btnAdd_Click(object sender, System.EventArgs e)
{
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto=UpPhoto.PostedFile;
int upPhotoLength=upPhoto.ContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto.InputStream;
PhotoStream.Read(PhotoArray,0,upPhotoLength); //连接数据库
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa"; SqlCommand cmd=new SqlCommand("UpdateImage",conn);
cmd.CommandType=CommandType.StoredProcedure; cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=PhotoArray; //如果你希望不使用存储过程来添加图片把上面四句代码改为:
//string strSql="Insert into test(FImage) values(@FImage)";
//SqlCommand cmd=new SqlCommand(strSql,conn);
//cmd.Parameters.Add("@FImage",SqlDbType.Image);
//cmd.Parameters["@FImage"].Value=PhotoArray; conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
在需要显示图片的地方添加如下代码:
<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image> ShowPhoto.aspx主体代码:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection()
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
string strSql="select * from test where id=2";//这里假设获取id为2的图片
SqlCommand cmd=new SqlCommand()
reader.Read();
Response.ContentType="application/octet-stream";
Response.BinaryWrite((Byte[])reader["FImage"]);
Response.End();
reader.Close();
}
}