读SQL里二进制图片时Bitmap的参数无效
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();
}