图片上传到SQLServer(二)

2014-11-24 07:22:24 · 作者: · 浏览: 9
" where id=" + context.Request.QueryString["id"];

SqlDataReader reader = cmd.ExecuteReader(

CommandBehavior.CloseConnection | CommandBehavior.SingleRow);

if (reader.Read())

{

byte[] imgbytes = null;

string imgtype = null;

if (reader.GetValue(0) != DBNull.Value)

{

imgbytes = (byte[])reader.GetValue(0);

imgtype = reader.GetString(1);

// If bmp, convert to jpg and show because of the different formation type.

if (imgtype.Equals("image/bmp", StringComparison.OrdinalIgnoreCase))

{

using (MemoryStream ms = new MemoryStream(imgbytes))

{

using (Bitmap bm = new Bitmap(Image.FromStream(ms)))

{

bm.Save(context.Response.OutputStream, ImageFormat.Jpeg);

}

}

}

else

{

context.Response.ContentType = imgtype;

context.Response.BinaryWrite(imgbytes);

}

}

else

{

imgbytes = File.ReadAllBytes(context.Server.MapPath

("~/DefaultImage/DefaultImage.JPG"));

imgtype = "image/pjpeg";

context.Response.ContentType = imgtype;

context.Response.BinaryWrite(imgbytes);

}

}

reader.Close();

context.Response.End();

}

}

public bool IsReusable

{

get

{

return false;

}

}

}