二进制 图片 数据库
// 上传的图片文件到Image
System.Drawing.Image img = System.Drawing.Image.FromFile(fileImg.PostedFile.FileName);
// 以Jpeg格式存到内存中
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
// 输出Response.BinaryWrite(二进制)
Response.ClearContent();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(ms.ToArray());
img.Dispose();
ms.Dispose();
ms.Flush();
-------------------------------------------------------------------------------------
// 上传的图片文件到Image
System.Drawing.Image img = System.Drawing.Image.FromFile(fileImg.PostedFile.FileName);
// 以Jpeg格式存到内存中
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.MDF;Integrated Security=True;User Instance=True"))
{
SqlCommand cmd = new SqlCommand("spInsertImage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ImageBinary", System.Data.SqlDbType.Image);
// 图片的二进制数据
cmd.Parameters["@ImageBinary"].Value = ms.ToArray();
con.Open();
try
{
// 调存储过程
cmd.ExecuteNonQuery();
Response.Write("ok");
}
catch
{
Response.Write("error");
}
}
img.Dispose();
ms.Dispose();
ms.Flush();
------------------------------------------------------------------------------
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.MDF;Integrated Security=True;User Instance=True"))
{
SqlCommand cmd = new SqlCommand("spSelectImage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ImageID", SqlDbType.Int);
// 在DropDownList中所选的ImageId
cmd.Parameters["@ImageID"].Value = Int32.Parse(ddlImage.SelectedValue);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
Response.ClearContent();
Response.ContentType = "image/jpeg";
while (sdr.Read())
{
// 读出相应记录的二进制并送显
Response.BinaryWrite((byte[])sdr[0]);
}
sdr.Close();
sdr.Dispose();
}
System.Drawing.Image img = System.Drawing.Image.FromFile(fileImg.PostedFile.FileName);
// 以Jpeg格式存到内存中
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
// 输出Response.BinaryWrite(二进制)
Response.ClearContent();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(ms.ToArray());
img.Dispose();
ms.Dispose();
ms.Flush();
-------------------------------------------------------------------------------------
// 上传的图片文件到Image
System.Drawing.Image img = System.Drawing.Image.FromFile(fileImg.PostedFile.FileName);
// 以Jpeg格式存到内存中
System.IO.MemoryStream ms = new System.IO.MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.MDF;Integrated Security=True;User Instance=True"))
{
SqlCommand cmd = new SqlCommand("spInsertImage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ImageBinary", System.Data.SqlDbType.Image);
// 图片的二进制数据
cmd.Parameters["@ImageBinary"].Value = ms.ToArray();
con.Open();
try
{
// 调存储过程
cmd.ExecuteNonQuery();
Response.Write("ok");
}
catch
{
Response.Write("error");
}
}
img.Dispose();
ms.Dispose();
ms.Flush();
------------------------------------------------------------------------------
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.MDF;Integrated Security=True;User Instance=True"))
{
SqlCommand cmd = new SqlCommand("spSelectImage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ImageID", SqlDbType.Int);
// 在DropDownList中所选的ImageId
cmd.Parameters["@ImageID"].Value = Int32.Parse(ddlImage.SelectedValue);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
Response.ClearContent();
Response.ContentType = "image/jpeg";
while (sdr.Read())
{
// 读出相应记录的二进制并送显
Response.BinaryWrite((byte[])sdr[0]);
}
sdr.Close();
sdr.Dispose();
}