读SQL里二进制图片时Bitmap的参数无效

caowenkai 2008-10-24 11:04:10
protected void btnSave_Click(object sender, EventArgs e)
{
int ProdID;
ProdID = int.Parse(txtProdID.Text);
//保存图片
this.SaveImageToDB(ProdID);
Response.Write("图片保存成功!");
}
/// <summary>
/// 保存图片入库
/// </summary>
/// <param name="ProdID"></param>
private void SaveImageToDB(int ProdID)
{
string imgName = string.Empty;
string imgContentType = string.Empty;
int imgLen;
byte[] imgbin;
string imgName2 = string.Empty;
string imgContentType2 = string.Empty;
byte[] imgbin2;
//检查文件是否存在
if (fUpLoadImage.PostedFile != null)
{
if (fUpLoadImage.PostedFile.FileName.Trim().Length > 0 && fUpLoadImage.PostedFile.ContentLength > 0)
{
Stream imgStream = fUpLoadImage.PostedFile.InputStream;//此位置imgStream出现异常报此流不支持超时,但是图片还是存到数据库,运行也正常。我跟踪的时候发现的。
imgLen = fUpLoadImage.PostedFile.ContentLength;
imgContentType = fUpLoadImage.PostedFile.ContentType;
imgName=fUpLoadImage.PostedFile.FileName.Substring(fUpLoadImage.PostedFile.FileName.LastIndexOf("\\") + 1);
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData, 0, imgLen);
imgbin = imgBinaryData;

//保存缩略图

imgName2 = "Thumb_" + imgName;
imgContentType2 = imgContentType;
imgbin2 = createThumnail(imgStream, 145, 145);

//保存图片到数据库

SqlConnection conn = new SqlConnection();
conn.ConnectionString = connstr;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insert_Product_image";

SqlParameter p1 = new SqlParameter("@ProdID", SqlDbType.Int);
SqlParameter p2 = new SqlParameter("@img_name", SqlDbType.VarChar, 50);
SqlParameter p3 = new SqlParameter("@img_data", SqlDbType.Image);
SqlParameter p4 = new SqlParameter("@img_contenttype", SqlDbType.VarChar, 50);
SqlParameter p5 = new SqlParameter("@img_name2", SqlDbType.VarChar, 50);
SqlParameter p6 = new SqlParameter("@img_data2", SqlDbType.Image);
SqlParameter p7 = new SqlParameter("@img_contenttype2", SqlDbType.VarChar, 50);

p1.Value = ProdID;
p2.Value = imgName;
p3.Value = imgbin;
p4.Value = imgContentType;
p5.Value = imgName2;
p6.Value = imgbin2;
p7.Value = imgContentType2;

cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.Parameters.Add(p4);
cmd.Parameters.Add(p5);
cmd.Parameters.Add(p6);
cmd.Parameters.Add(p7);

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

}
}
}
以上为存图片的代码,以下代码为读图片:ShowImage.aspx
if (!IsPostBack)
{
this.GetPhoto(1, 400);
}
/// <summary>
/// 经过处理获取所略图
/// </summary>
/// <param name="ms"></param>
/// <param name="height"></param>
/// <returns></returns>
private byte[] LessonPicAutoWidth(MemoryStream ms, int height)
{
byte[] ChagedByte;
System.Drawing.Image OriginalImage = System.Drawing.Image.FromStream(ms, true);
System.Drawing.Bitmap OriginalPic, NewPic;
OriginalPic = new System.Drawing.Bitmap(OriginalImage);

int Width = height * OriginalPic.Width / OriginalPic.Height;

if (Width > 280)
{
Width = 280;
}

NewPic = new Bitmap(OriginalPic, Width, height);
MemoryStream Newms = new MemoryStream();
NewPic.Save(Newms, System.Drawing.Imaging.ImageFormat.Jpeg);
ChagedByte = Newms.GetBuffer();
OriginalPic.Dispose();
NewPic.Dispose();
Newms.Close();

return ChagedByte;
}
/// <summary>
/// 显示图片
/// </summary>
/// <param name="EmployeeID"></param>
public void GetPhoto(int ProdID, int height)
{
Response.Expires = -9999;
Response.AddHeader("pragma", "no-cache");
Response.AddHeader("cache-ctrol", "no-cache");
Response.ContentType = "image/jpeg";
Bitmap bmp;
byte[] PhotoByte;
string sql = string.Format("Select img_data From insert_Product_image Where ProdID={0}", ProdID);

using (SqlConnection conn = new SqlConnection(connstr))
{
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = sql;
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
reader.Read();
PhotoByte = (byte[])(reader.GetValue(0));
}
}
}
if (PhotoByte == null || PhotoByte.Length == 0) return;

MemoryStream tempStream = new MemoryStream(PhotoByte);//此位置的tempStream 也出现异常报此流不支持超时,也是我跟踪的时候发现的。
bmp = new Bitmap(tempStream);//这里就出错了,报tempStream参数无效。程序终止。

Response.ContentType = "image/jpeg";
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
}
...全文
912 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
晓景 2009-03-16
  • 打赏
  • 举报
回复
数据库在读出时文件被破坏了。
fanshengrui 2009-03-05
  • 打赏
  • 举报
回复
我估计你的软件错误,我也经历了,不过你可以去下一个单机的一种开发语言环境去按照上面的方法就可以了!
heshl 2009-01-31
  • 打赏
  • 举报
回复
我也是同样问题,参数无效的原因就是MemoryStream不支持超时,我想可能得重写下MemoryStream.
wh469696366 2008-11-29
  • 打赏
  • 举报
回复
有人有好的解决办法吗??
冷月孤峰 2008-10-27
  • 打赏
  • 举报
回复
baidu一下很多
http://www.cnblogs.com/nacarat/articles/912138.html
caowenkai 2008-10-27
  • 打赏
  • 举报
回复
难道一个CSDN没有一个会 数据库图片存和读的?
caowenkai 2008-10-26
  • 打赏
  • 举报
回复
来几个会的帮帮忙啊!
huwenting 2008-10-24
  • 打赏
  • 举报
回复
帮忙顶下.此贴不要沉,我也有这个问题!
caowenkai 2008-10-24
  • 打赏
  • 举报
回复
同样还是报参数无效

MemoryStream tempStream = new MemoryStream(PhotoByte);
tempStream.Position = 0;

bmp = new Bitmap(tempStream);
caowenkai 2008-10-24
  • 打赏
  • 举报
回复
我这样写了不行啊

MemoryStream tempStream = new MemoryStream(PhotoByte);
tempStream.Position = 0;

bmp = new Bitmap(tempStream);
mjjzg 2008-10-24
  • 打赏
  • 举报
回复
路过,捧个场,顶一下
greystar 2008-10-24
  • 打赏
  • 举报
回复
MemoryStream tempStream = new MemoryStream(PhotoByte);//此位置的tempStream 也出现异常报此流不支持超时,也是我跟踪的时候发现的。

tempStream.Position=0;


bmp = new Bitmap(tempStream);//这里就出错了,报tempStream参数无效。程序终止。
houlinghouling 2008-10-24
  • 打赏
  • 举报
回复
图片保存到数据库的方法:

public void imgToDB(string sql)
{ //参数sql中要求保存的imge变量名称为@images
//调用方法如:imgToDB("update UserPhoto set Photo=@images where UserNo='" + temp + "'");
FileStream fs = File.OpenRead(t_photo.Text);
byte[] imageb = new byte[fs.Length];
fs.Read(imageb, 0, imageb.Length);
fs.Close();
SqlCommand com3 = new SqlCommand (sql,con);
com3.Parameters.Add("@images", SqlDbType.Image).Value = imageb;
if (com3.Connection.State == ConnectionState.Closed)
com3.Connection.Open();
try
{
com3.ExecuteNonQuery();
}
catch
{ }
finally
{ com3.Connection.Close(); }
}

数据库中读出图片并显示在picturebox中:

方法一:
private void ShowImage(string sql)
{
//调用方法如:ShowImage("select Photo from UserPhoto where UserNo='" + userno +"'");
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
byte[] b= (byte[])cmd.ExecuteScalar();
if (b.Length 〉 0)
{
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
pictureBox1.Image = new Bitmap(stream);
stream.Close();
}
conn.Close();
}

方法二:当在dg中选中某行时:
private void dg_MouseUp(object sender, MouseEventArgs e)
{
//整行选择
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{//用户编号,姓名,性别,身份证号,籍贯,学院,系所,校区,部门,电话,照片
//显示相片
object imgobj=dg[10, dg.CurrentRow.Index].Value;
if (imgobj != null && !Convert.IsDBNull(imgobj))
{
byte[] imgb = (byte[])imgobj;
MemoryStream memStream = new MemoryStream(imgb);
try
{
Bitmap myimge = new Bitmap(memStream);
this.pictureBox1.Image = myimge;
}
catch
{
DB.msgbox("从数据库读取相片失败!");
}
}
else
pictureBox1.Image = null;
}
}
greystar 2008-10-24
  • 打赏
  • 举报
回复
tempStream.Position=0;
caowenkai 2008-10-24
  • 打赏
  • 举报
回复
没一个人会吗?会的帮帮忙,谢谢.很急!

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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