Image.FromStream参数无效
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Image.FromStream参数无效相关的知识,希望对你有一定的参考价值。
SqlConnection con = new SqlConnection("server=.;database=xiaoluo1;integrated security=true");
SqlCommand cmd = new SqlCommand("select * from photo", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
MemoryStream ms = new MemoryStream((byte[])reader["照片"]);
Image image = Image.FromStream(ms);
reader.Close();
con.Close();
pictureBox1.Image = image;
MemoryStream stream = new MemoryStream();
PictureBox1.Image.Save(stream, ImageFormat.Bmp ); //这里出错,原图片格式是JPEG
byte [] bytestream = new byte [ stream.Length ];
stream .Read (bytestream ,0,bytestream .Length );
stream.Flush();
//插入数据库新记录
DataAdapter.InsertQuery(bytestream);
这样,读数据显示的时候就出现了Image.FromStream参数无效的错误。
多次测试后发现是自作聪明定义了流转换的格式,改成下面这样后,读数据库图片时候参数错误的问题就消失了:
MemoryStream stream = new MemoryStream();
PictureBox1.Image.Save(stream,PictureBox1 .Image .RawFormat );
byte[] bytestream = stream.ToArray();
stream.Flush();
//插入数据库新记录
DataAdapter.InsertQuery(bytestream); 参考技术A
FromStream 异常,说明传入的stream参数无效,你需要确认,你的图片流是有效的本回答被提问者采纳 参考技术B 我也遇到这个问题了,纠结好几天了,还是没有解决,ms明明取到值了,可是在FromStream中还是无效
Image.FromStream 参数无效
想实现数据库存取图片,保存时没事,读取时用下面的方法
byte[] mydata = ((byte[])(/*数据库返回值*/));
MemoryStream ms = new MemoryStream(mydata);
ms.Position = 0;
ms.Read(mydata, 0, Convert.ToInt32(ms.Length));
Image img = Image.FromStream(ms);
ms.Close();
Image img = Image.FromStream(ms);这一句提示参数无效,怎么办啊
有没有别的简单的数据库存取图片方法也行的
//'*******************************************
int filelength;
HttpPostedFile upfile;
upfile = lofile.PostedFile;
filelength = upfile.ContentLength; //记录文件长度
byte[] FileByteArray = new Byte[filelength]; //图象文件临时储存Byte数组
Stream StreamObject = upfile.InputStream; //建立数据流对像
StreamObject.Read(FileByteArray, 0, filelength);
filesize = upfile.ContentLength;
filelx = upfile.ContentType.ToString();
//*******************************************
//读取文件
filename = myset2.Tables["temps"].Rows[0]["SysS_FileName"].ToString();
filesize = int.Parse(myset2.Tables["temps"].Rows[0]["SysS_FileSize"].ToString());
filetype = myset2.Tables["temps"].Rows[0]["SysS_FileType"].ToString();
//Dim fileimge(filesize) As Byte
//fileimge = myset2.Tables("temps").Rows(0).Item("SysS_FileImage")
byte[] fileimge = new Byte[filesize];
fileimge = (byte[])myset2.Tables["temps"].Rows[0]["SysS_FileImage"];
Response.ContentType = filetype; //设定输出文件类型
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "inline; filename=" + HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(filename)));//输出方式,在浏览器打开.
//Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(filename)));//以文件方式打开
Response.BinaryWrite(fileimge);
// Response.OutputStream.Write(fileimge, 0, filesize)
myset2.Dispose();
以上是关于Image.FromStream参数无效的主要内容,如果未能解决你的问题,请参考以下文章
Image.FromStream(stream);报参数无效
Image.FromStream(PostedFile.InputStream) 失败。 (参数无效。)(AsyncFileUpload))