如何在C#中把数据库里的二进制图片转换成jpg格式 我做了一个把图片转换成二进制存入oracle数据库的程序,我想把它从数据库里读出来并显示出来,希望是在后台写代码,请高手们帮帮忙! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Image.Save( Stream stream, ImageFormat format); TypeConverter.ConvertFrom public object ConvertFrom( object value);将给定值转换为此转换器的类型。 hdt(倦怠) 你能说清楚点吗,我是新手,谢谢 void savejpg(String NAME){ SqlConnection CON = new SqlConnection(); SqlCommand CMD = new SqlCommand("SQL"); FileStream fs; BinaryWriter bw; int bufferSize = 1024; byte[] outbyte = new byte[bufferSize]; long retval; long startIndex = 0; CON.Open(); SqlDataReader myReader = CMD.ExecuteReader(CommandBehavior.SequentialAccess); while (myReader.Read()) { fs = new FileStream(NAME, FileMode.OpenOrCreate, FileAccess.Write); bw = new BinaryWriter(fs); startIndex = 0; retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize); while (retval > 0) { bw.Write(outbyte); bw.Flush(); startIndex += bufferSize; retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize); } bw.Write(outbyte); bw.Flush(); bw.Close(); fs.Close(); } myReader.Close(); CON.Close(); }类似 从数据读图片代码比较长就不写了byte[] barrImage = 数据库存图片的二进制字段值MemoryStream ms = new MemoryStream( barrImage );Image image = Image.FormStream(ms);image.Save( Response.OutputStream , ImageFormat.Jpeg ) Image 的引用为什么总是报错,不是using System.Drawing.Imaging;吗 命名空间: System.Drawing 为什么还是报Image是不明确的引用 c:\inetpub\wwwroot\WebApporacle\WebForm1.aspx.cs(51): 找不到类型或命名空间名称“image”(是否缺少 using 指令或程序集引用?) c:\inetpub\wwwroot\WebApporacle\WebForm1.aspx.cs(50): “Image”是不明确的引用 考虑是否你的程序里有叫Image的类另用System.Drawing.Image 代替Image 用System.Drawing.Image 代替Image应该不会再报不明确的引用 private Image RenderImage(byte[] byteImg){System.Drawing.Image image1 = System.Drawing.Image.FromStream(new MemoryStream(byteImg));return image1;} System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true);使用了无效参数。 大家帮我 看看代码吗public string bind() { StringBuilder sb=new StringBuilder(); strsql="select docid,fileid,rowed,filecon from docmapfile"; OracleDataAdapter da = new OracleDataAdapter(strsql,conn); da.Fill(ds); this.text1.Value=ds.Tables[0].Rows[0].ItemArray[0].ToString(); this.text2.Value=ds.Tables[0].Rows[0].ItemArray[1].ToString(); this.text3.Value=ds.Tables[0].Rows[0].ItemArray[2].ToString(); byte[] barrImage=(byte[])ds.Tables[0].Rows[0].ItemArray[3]; MemoryStream ms = new MemoryStream(barrImage); System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true); image.Save( Response.OutputStream , ImageFormat.Jpeg ); sb.Append(image.ToString()); conn.Close(); return sb.ToString(); } MemoryStream ms = new MemoryStream( barrImage );Image image = Image.FormStream(ms,true);image.Save( Response.OutputStream ,ImageFormat.Jpeg )关键就是这里 我就是System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true);这里报错,使用了无效参数。 to 我就是System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true);这里报错,使用了无效参数。我刚遇到了个类似的错误,请检查一下你的ms是否已经保存了内容.可能是你的ms根本没有取到值. 插入时把图片数据作为Byte数组插入数据库,下面是把文件转换成数组private byte[] ReadImageData(string strFileName){ byte[] BlobData = null; Bitmap picture; FileStream stream; stream = new FileStream(strFileName,FileModel.Open,FileAccess.Read); BlobData = new byte[stream.Length]; stream.Read(BlobData,0,(int)stream.Length); picture = new Bitmap(stream); stream.Close(); return BlobData;}从数据库中读取图片文件数据转换成byte数组,下面是如何将数组转换为图片private Bitmap GetBitmap(byte[] img){ if(img != null) { MemoryStream ms = new MemoryStream(img,true); ms.Read(img,0,img.Length); Bitmap bmp = new Bitmap(ms); ms.Close(); return bmp; } else return null;} this.oracleConnection1.Open();byte[] pic = (byte[])ocm.ExecuteScalar();//这个命令只返回图象数据this.oracleConnection1.Close();System.IO.MemoryStream ms = new System.IO.MemoryStream(pic);System.Drawing.Image img = System.Drawing.Image.FromStream(ms);this.pictureBox1.Image = img; to 我的ms里面有值,我调试看了的,除了这里,那里还有错吗?如果你的ms中有值,就不应该提示这个错误了,它还报什么错? public void bindphoto(string xh,PictureBox pictureBox1) { dbcontroller obj=new dbcontroller(); string sql="SELECT * FROM photo WHERE xh='" + xh + "'"; obj.open(); DataSet myphotods = obj.GetDs(sql); obj.close(); if (myphotods.Tables[0].Rows.Count > 0) { byte[] bytephoto = new byte[0]; bytephoto = (byte[])(myphotods.Tables[0].Rows[0]["ZP"]); MemoryStream stmPhoto = new MemoryStream(bytephoto); pictureBox1.Image = Image.FromStream(stmPhoto); } else { pictureBox1.Image = null; } } 我是用winform作的图片存入数据库,然后通过web显示出来 是CS模式的,还是BS模式的程序? 哦,这我倒没试过,我这有一个用OPENDIALOG控件(CS模式下)和使用UploadFile控件(BS模式下)的存图片的函数!//============================================================= // 函 数 名:Savephoto // 功能描述:添加或修改照片 // 输入参数:学生学号,已经打开文件的openFileDialog // 返 回 值:无 //============================================================= public void Savephoto(string xh,OpenFileDialog openFileDialog1) { //定义byte类型变量 byte[] myblob = null; Stream fs; //给变量blob赋值 fs =openFileDialog1.OpenFile(); if(fs!=null) { openFileDialog1.Reset(); myblob = new byte[fs.Length]; fs.Read(myblob,0,int.Parse(myblob.Length.ToString())); fs.Close(); OracleDataAdapter myoda; OracleCommand cmd; DataSet myds = new DataSet(); DataTable mydt = new DataTable(); DataRow mydr; string connstr="User ID=student;Data Source=hres;Password=studentmanage"; OracleConnection conn= new OracleConnection(connstr); string selesql; selesql = "SELECT * FROM photo WHERE xh='" + xh + "'"; cmd = new OracleCommand(selesql,conn); myoda = new OracleDataAdapter(selesql,conn); myoda.SelectCommand = cmd; myoda.Fill(myds, "PHOTO"); string info = ""; if (myds.Tables[0].Rows.Count > 0)//修改照片 { info = "修改照片成功"; string updatesql = "UPDATE photo SET zp=:vPHOTO WHERE xh='" + xh + "'"; OracleCommand updatecmd = new OracleCommand(updatesql,conn); myoda.UpdateCommand = updatecmd; myoda.UpdateCommand.Parameters.Add(":vPHOTO", OracleType.Blob, myblob.Length, "ZP"); myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey; myoda.FillSchema(myds, SchemaType.Source, "PHOTO"); myoda.Fill(myds, "PHOTO"); mydt = myds.Tables[0]; mydr = mydt.Rows[0]; string a = mydr["XH"].ToString(); mydr.BeginEdit(); mydr["XH"] = xh; mydr["ZP"] = myblob; mydr.EndEdit(); myoda.Update(myds, "PHOTO"); } else//上传新照片 { info = "上传新照片成功"; string insertsql = "INSERT INTO photo (xh,zp) VALUES ('" + xh + "',:vPHOTO)"; OracleCommand insertcmd = new OracleCommand(insertsql,conn); myoda.InsertCommand = insertcmd; myoda.InsertCommand.Parameters.Add(":vPHOTO", OracleType.Blob, myblob.Length, "ZP"); myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey; myoda.FillSchema(myds, SchemaType.Source, "PHOTO"); myoda.Fill(myds, "PHOTO"); mydt = myds.Tables[0]; mydr = mydt.NewRow(); mydr.BeginEdit(); mydr["XH"] = xh; mydr["ZP"] = myblob; mydr.EndEdit(); mydt.Rows.Add(mydr); myoda.Update(myds, "PHOTO"); } MessageBox.Show(info); } } /// 上传图片 private void SavePhoto(string xh) { if(IsAllowedExtension(UploadFile)) { //如果是修改图片,则将修改前图片删掉 string[] arrExtension = {".gif",".jpg",".jpeg",".bmp",".png"}; string path1=""; //判断该扩展名是否合法 for(int i = 0; i< arrExtension.Length; i++) { string fileName1=TextBox_xh.Text; path1=Server.MapPath(".") + @"\Photo\" +fileName1+arrExtension[i]; if(File.Exists(path1)) { FileInfo delete=new FileInfo(path1); delete.Delete(); break; } } //上传图片 string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToLower();// string extension=".jpg"; string fileName=TextBox_xh.Text; string path = Server.MapPath(".") + @"\Photo\" + fileName + extension; //保存图片到服务器目录下 UploadFile.PostedFile.SaveAs(path); //得到图片的二进制形式 HttpPostedFile upFile=UploadFile.PostedFile; //得到上传文件的长度; int upFileLength=upFile.ContentLength; string ContentType=upFile.ContentType; byte[] bPicture; bPicture=new byte[upFileLength]; Stream filestream=upFile.InputStream ; filestream.Read (bPicture,0,upFileLength); filestream.Close (); OracleDataAdapter myoda; DataSet myds = new DataSet(); System.Data.DataTable mydt = new System.Data.DataTable(); DataRow mydr; OracleCommand cmd; string connstr="User ID=student;Data Source=hers;Password=studentmanage"; OracleConnection conn= new OracleConnection(connstr); string selesql = "SELECT * FROM photo WHERE xh='" + xh + "'"; cmd = new OracleCommand(selesql,conn); myoda = new OracleDataAdapter(selesql,conn); myoda.SelectCommand = cmd; myoda.Fill(myds, "PHOTO"); string insertsql = "INSERT INTO photo (xh,zp) VALUES ('" + xh + "',:vPHOTO)"; string updatesql = "UPDATE photo SET zp=:vPHOTO WHERE xh='" + xh + "'"; try { if(myds.Tables[0].Rows.Count > 0) { OracleCommand updatecmd = new OracleCommand(updatesql,conn); myoda.UpdateCommand = updatecmd; myoda.UpdateCommand.Parameters.Add(":vPHOTO", OracleType.Blob, bPicture.Length, "ZP"); myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey; myoda.FillSchema(myds, SchemaType.Source, "PHOTO"); myoda.Fill(myds, "PHOTO"); mydt = myds.Tables[0]; mydr = mydt.Rows[0]; string a = mydr["XH"].ToString(); mydr.BeginEdit(); mydr["XH"] = xh; mydr["ZP"] = bPicture; mydr.EndEdit(); myoda.Update(myds, "PHOTO"); pictureBox1.ImageUrl=path; } else { OracleCommand insertcmd = new OracleCommand(insertsql, conn); myoda.InsertCommand = insertcmd; myoda.InsertCommand.Parameters.Add(":vPHOTO", OracleType.Blob,bPicture.Length, "ZP"); myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey; myoda.FillSchema(myds, SchemaType.Source, "PHOTO"); myoda.Fill(myds, "PHOTO"); mydt = myds.Tables[0]; mydr = mydt.NewRow(); mydr.BeginEdit(); mydr["XH"] = xh; mydr["ZP"] = bPicture ; mydr.EndEdit(); mydt.Rows.Add(mydr); myoda.Update(myds, "PHOTO"); pictureBox1.ImageUrl=path; } } catch { throw; } } } ArcGIS BorderSelectorClass所在的命名空间? 页面Ajax异步获取数据问题,在线等! C#如何调用C++写的DLL接口 关于在C# Windows服务中启动 exe 文件, 不能显示出exe文件的窗体的问题. 请帮助! 关于网络监控的问题 c# 怎么做卸载程序 关于模拟用户登录的问题impersonate 100分求一个换皮肤软件的详细代码,请高手赐教。 用C#如何查找任务管理里的进程?有源代码嘛? 请问editplus+.net哪里有下载? 请教一个关于C#实现邮件发送的问题 请问如何将数据库中的数据存到数组中?
Stream stream,
ImageFormat format
);
public object ConvertFrom(
object value
);
将给定值转换为此转换器的类型。
{
SqlConnection CON = new SqlConnection();
SqlCommand CMD = new SqlCommand("SQL"); FileStream fs;
BinaryWriter bw; int bufferSize = 1024;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0; CON.Open();
SqlDataReader myReader = CMD.ExecuteReader(CommandBehavior.SequentialAccess); while (myReader.Read())
{
fs = new FileStream(NAME, FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs); startIndex = 0;
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize); while (retval > 0)
{
bw.Write(outbyte);
bw.Flush();
startIndex += bufferSize;
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
} bw.Write(outbyte);
bw.Flush(); bw.Close();
fs.Close();
} myReader.Close();
CON.Close(); }
类似
就不写了byte[] barrImage = 数据库存图片的二进制字段值
MemoryStream ms = new MemoryStream( barrImage );
Image image = Image.FormStream(ms);
image.Save( Response.OutputStream , ImageFormat.Jpeg )
命名空间: System.Drawing
另用System.Drawing.Image 代替Image
{
System.Drawing.Image image1 = System.Drawing.Image.FromStream(new MemoryStream(byteImg));
return image1;
}
使用了无效参数。
public string bind()
{
StringBuilder sb=new StringBuilder();
strsql="select docid,fileid,rowed,filecon from docmapfile";
OracleDataAdapter da = new OracleDataAdapter(strsql,conn);
da.Fill(ds);
this.text1.Value=ds.Tables[0].Rows[0].ItemArray[0].ToString();
this.text2.Value=ds.Tables[0].Rows[0].ItemArray[1].ToString();
this.text3.Value=ds.Tables[0].Rows[0].ItemArray[2].ToString(); byte[] barrImage=(byte[])ds.Tables[0].Rows[0].ItemArray[3];
MemoryStream ms = new MemoryStream(barrImage);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true);
image.Save( Response.OutputStream , ImageFormat.Jpeg );
sb.Append(image.ToString()); conn.Close();
return sb.ToString();
}
Image image = Image.FormStream(ms,true);
image.Save( Response.OutputStream ,ImageFormat.Jpeg )
关键就是这里
这里报错,使用了无效参数。
这里报错,使用了无效参数。我刚遇到了个类似的错误,请检查一下你的ms是否已经保存了内容.
可能是你的ms根本没有取到值.
private byte[] ReadImageData(string strFileName)
{
byte[] BlobData = null;
Bitmap picture;
FileStream stream;
stream = new FileStream(strFileName,FileModel.Open,FileAccess.Read);
BlobData = new byte[stream.Length];
stream.Read(BlobData,0,(int)stream.Length);
picture = new Bitmap(stream);
stream.Close();
return BlobData;
}从数据库中读取图片文件数据转换成byte数组,下面是如何将数组转换为图片private Bitmap GetBitmap(byte[] img)
{
if(img != null)
{
MemoryStream ms = new MemoryStream(img,true);
ms.Read(img,0,img.Length);
Bitmap bmp = new Bitmap(ms);
ms.Close();
return bmp;
}
else
return null;
}
byte[] pic = (byte[])ocm.ExecuteScalar();//这个命令只返回图象数据
this.oracleConnection1.Close();
System.IO.MemoryStream ms = new System.IO.MemoryStream(pic);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
this.pictureBox1.Image = img;
它还报什么错?
{
dbcontroller obj=new dbcontroller();
string sql="SELECT * FROM photo WHERE xh='" + xh + "'";
obj.open();
DataSet myphotods = obj.GetDs(sql);
obj.close();
if (myphotods.Tables[0].Rows.Count > 0)
{
byte[] bytephoto = new byte[0];
bytephoto = (byte[])(myphotods.Tables[0].Rows[0]["ZP"]);
MemoryStream stmPhoto = new MemoryStream(bytephoto);
pictureBox1.Image = Image.FromStream(stmPhoto);
}
else
{
pictureBox1.Image = null;
}
}
//=============================================================
// 函 数 名:Savephoto
// 功能描述:添加或修改照片
// 输入参数:学生学号,已经打开文件的openFileDialog
// 返 回 值:无
//=============================================================
public void Savephoto(string xh,OpenFileDialog openFileDialog1)
{
//定义byte类型变量
byte[] myblob = null;
Stream fs;
//给变量blob赋值
fs =openFileDialog1.OpenFile();
if(fs!=null)
{
openFileDialog1.Reset();
myblob = new byte[fs.Length];
fs.Read(myblob,0,int.Parse(myblob.Length.ToString()));
fs.Close();
OracleDataAdapter myoda;
OracleCommand cmd;
DataSet myds = new DataSet();
DataTable mydt = new DataTable();
DataRow mydr;
string connstr="User ID=student;Data Source=hres;Password=studentmanage";
OracleConnection conn= new OracleConnection(connstr);
string selesql;
selesql = "SELECT * FROM photo WHERE xh='" + xh + "'";
cmd = new OracleCommand(selesql,conn);
myoda = new OracleDataAdapter(selesql,conn);
myoda.SelectCommand = cmd;
myoda.Fill(myds, "PHOTO");
string info = "";
if (myds.Tables[0].Rows.Count > 0)//修改照片
{
info = "修改照片成功";
string updatesql = "UPDATE photo SET zp=:vPHOTO WHERE xh='" + xh + "'";
OracleCommand updatecmd = new OracleCommand(updatesql,conn);
myoda.UpdateCommand = updatecmd;
myoda.UpdateCommand.Parameters.Add(":vPHOTO", OracleType.Blob, myblob.Length, "ZP"); myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
myoda.Fill(myds, "PHOTO"); mydt = myds.Tables[0];
mydr = mydt.Rows[0];
string a = mydr["XH"].ToString(); mydr.BeginEdit();
mydr["XH"] = xh;
mydr["ZP"] = myblob;
mydr.EndEdit();
myoda.Update(myds, "PHOTO");
}
else//上传新照片
{
info = "上传新照片成功";
string insertsql = "INSERT INTO photo (xh,zp) VALUES ('" + xh + "',:vPHOTO)";
OracleCommand insertcmd = new OracleCommand(insertsql,conn);
myoda.InsertCommand = insertcmd;
myoda.InsertCommand.Parameters.Add(":vPHOTO", OracleType.Blob, myblob.Length, "ZP"); myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
myoda.Fill(myds, "PHOTO"); mydt = myds.Tables[0];
mydr = mydt.NewRow(); mydr.BeginEdit();
mydr["XH"] = xh;
mydr["ZP"] = myblob;
mydr.EndEdit();
mydt.Rows.Add(mydr);
myoda.Update(myds, "PHOTO");
}
MessageBox.Show(info);
}
}
private void SavePhoto(string xh)
{
if(IsAllowedExtension(UploadFile))
{
//如果是修改图片,则将修改前图片删掉
string[] arrExtension = {".gif",".jpg",".jpeg",".bmp",".png"};
string path1="";
//判断该扩展名是否合法
for(int i = 0; i< arrExtension.Length; i++)
{
string fileName1=TextBox_xh.Text;
path1=Server.MapPath(".") + @"\Photo\" +fileName1+arrExtension[i];
if(File.Exists(path1))
{
FileInfo delete=new FileInfo(path1);
delete.Delete();
break;
}
}
//上传图片
string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToLower();// string extension=".jpg";
string fileName=TextBox_xh.Text;
string path = Server.MapPath(".") + @"\Photo\" + fileName + extension;
//保存图片到服务器目录下
UploadFile.PostedFile.SaveAs(path);
//得到图片的二进制形式
HttpPostedFile upFile=UploadFile.PostedFile;
//得到上传文件的长度;
int upFileLength=upFile.ContentLength;
string ContentType=upFile.ContentType;
byte[] bPicture;
bPicture=new byte[upFileLength];
Stream filestream=upFile.InputStream ;
filestream.Read (bPicture,0,upFileLength);
filestream.Close ();
OracleDataAdapter myoda;
DataSet myds = new DataSet();
System.Data.DataTable mydt = new System.Data.DataTable();
DataRow mydr;
OracleCommand cmd;
string connstr="User ID=student;Data Source=hers;Password=studentmanage";
OracleConnection conn= new OracleConnection(connstr);
string selesql = "SELECT * FROM photo WHERE xh='" + xh + "'";
cmd = new OracleCommand(selesql,conn);
myoda = new OracleDataAdapter(selesql,conn);
myoda.SelectCommand = cmd;
myoda.Fill(myds, "PHOTO");
string insertsql = "INSERT INTO photo (xh,zp) VALUES ('" + xh + "',:vPHOTO)";
string updatesql = "UPDATE photo SET zp=:vPHOTO WHERE xh='" + xh + "'";
try
{
if(myds.Tables[0].Rows.Count > 0)
{
OracleCommand updatecmd = new OracleCommand(updatesql,conn);
myoda.UpdateCommand = updatecmd;
myoda.UpdateCommand.Parameters.Add(":vPHOTO", OracleType.Blob, bPicture.Length, "ZP");
myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
myoda.Fill(myds, "PHOTO");
mydt = myds.Tables[0];
mydr = mydt.Rows[0];
string a = mydr["XH"].ToString();
mydr.BeginEdit();
mydr["XH"] = xh;
mydr["ZP"] = bPicture;
mydr.EndEdit();
myoda.Update(myds, "PHOTO");
pictureBox1.ImageUrl=path;
}
else
{
OracleCommand insertcmd = new OracleCommand(insertsql, conn);
myoda.InsertCommand = insertcmd;
myoda.InsertCommand.Parameters.Add(":vPHOTO", OracleType.Blob,bPicture.Length, "ZP");
myoda.MissingSchemaAction = MissingSchemaAction.AddWithKey;
myoda.FillSchema(myds, SchemaType.Source, "PHOTO");
myoda.Fill(myds, "PHOTO");
mydt = myds.Tables[0];
mydr = mydt.NewRow();
mydr.BeginEdit();
mydr["XH"] = xh;
mydr["ZP"] = bPicture ;
mydr.EndEdit();
mydt.Rows.Add(mydr);
myoda.Update(myds, "PHOTO");
pictureBox1.ImageUrl=path;
}
}
catch
{
throw;
}
}
}