圖像文件的讀取,直接寫入流
private void ImgDataRead()
{
int ImgID = Convert.ToInt32(Request.QueryString["id"]);
SqlConnection Con = new SqlConnection (System.Configuration.ConfigurationSettings.APPSettings["ConnectionString"]);
String SqlCmd = "SELECT * FROM ImageStore WHERE ID = @ImageID";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
Response.ContentType = (string)SqlReader["ImageContentType"];//設定輸出文件類型
//輸出圖象文件二進制數制
Response.OutputStream.Write((byte[])SqlReader["ImageData"],0,Convert.ToInt32(SqlReader ["ImageSize"]));
Response.BufferOutput = true;
//或 byte[] bytes= (byte[])SqlReader["ImageData"];
// MemoryStream memStream=new MemoryStream(bytes);
// try
// {
// Bitmap myImage = new Bitmap(memStream);
// this.pictureBox1.Image= myImage;
// }
// catch
// {
// this.pictureBox1.Image=null;
// }
Con.Close();
}