数据库读取和保存图片,及在CS、BS上显示图片的不同方法
//从数据库中读取图片
pubilc Bitmap GetImageFromDb()
{
string strConn = "...";
DBManager dbManager = new DBManager(); //数据库操作类
dbManager.SetConnectionString(strConn); //设置数据库连接字符串
string strSelect = "..."; //查询字符串,其中包括图片字段
SqlDataReader reader = dbManager.GetDataReader(strSelect);
byte[] buffer = null;
if(reader.Read())
{
buffer = (byte[])reader["image"];
}
MemoryStream stream = null;
try
{
stream = new MemoryStream(buffer);
Bitmap image = new Bitmap(stream);
return image;
}
catch()
{
return null;
}
finally
{
reader.close();
dbManager.close();
}
}
//保存图片到数据库中
public int StoreImageToDb()
{
string fileName = "..."; //图片路径
FileStream stream = new FileStream(fileName);
byte[] buffer = new byte[stream.Length]
stream.Read(buffer,0,buffer.Length);
string strConn = "...";
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("sp_name",conn);
cmd.CommandType = CommandType.StoredProcedure; //使用存储过程
SqlParameter param = new SqlParameter("@imageFieldName", SqlDbType.Image);
param.Value = buffer;
cmd.Parameters.Add(param);
try
{
return cmd.ExecuteNonQuery();
}
catch()
{
}
finally
{
conn.Close();
}
}
//CS 在CS中通过对PictureBox的直接赋值就可以更新图片。pictureBox1.Image = GetImageFromDb();
//在BS中由于<asp:Image>控件没有直接Image属性,只有ImageUrl,那么就有点复杂了。如下是动态的图片更新代码。
//创建一个.ashx页面,添加代码如下:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
int dptId = -1;
Stream stream = null;
if (context.Request.QueryString["dptId"] != null &&
context.Request.QueryString["dptId"] != ""
{
dptId = Convert.ToInt32(context.Request.QueryString["dptId"]);
stream = GetImage(dptId);
}
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0)
{
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
context.Response.End();
}
public Stream GetImage(int dptId)
{
DbManager dbManager = new DbManager();
dbManager.SetDefaultConnectionString();
string strSelect = "select * from pb_depart where DptId='" + dptId + "'";
SqlDataReader reader = dbManager.GetDataReader(strSelect);
byte[] buffer = null;
if (reader.Read())
{
buffer = (byte[])reader["Logo"];
}
MemoryStream stream = null;
try
{
stream = new MemoryStream(buffer);
return stream;
}
catch (ArgumentNullException e)
{
return null;
}
finally
{
reader.Close();
dbManager.Close();
}
}
在web页面中对<asp:Image>进行绑定操作
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?PhotoID=" + Eval("dptId" %>' />
或者在.cs文件中对<asp:Image>的ImageUrl属性进行直接赋值,Image1.ImageUrl = "Handler.ashx?dptId=" + dptId.ToString();
pubilc Bitmap GetImageFromDb()
{
string strConn = "...";
DBManager dbManager = new DBManager(); //数据库操作类
dbManager.SetConnectionString(strConn); //设置数据库连接字符串
string strSelect = "..."; //查询字符串,其中包括图片字段
SqlDataReader reader = dbManager.GetDataReader(strSelect);
byte[] buffer = null;
if(reader.Read())
{
buffer = (byte[])reader["image"];
}
MemoryStream stream = null;
try
{
stream = new MemoryStream(buffer);
Bitmap image = new Bitmap(stream);
return image;
}
catch()
{
return null;
}
finally
{
reader.close();
dbManager.close();
}
}
//保存图片到数据库中
public int StoreImageToDb()
{
string fileName = "..."; //图片路径
FileStream stream = new FileStream(fileName);
byte[] buffer = new byte[stream.Length]
stream.Read(buffer,0,buffer.Length);
string strConn = "...";
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("sp_name",conn);
cmd.CommandType = CommandType.StoredProcedure; //使用存储过程
SqlParameter param = new SqlParameter("@imageFieldName", SqlDbType.Image);
param.Value = buffer;
cmd.Parameters.Add(param);
try
{
return cmd.ExecuteNonQuery();
}
catch()
{
}
finally
{
conn.Close();
}
}
//CS 在CS中通过对PictureBox的直接赋值就可以更新图片。pictureBox1.Image = GetImageFromDb();
//在BS中由于<asp:Image>控件没有直接Image属性,只有ImageUrl,那么就有点复杂了。如下是动态的图片更新代码。
//创建一个.ashx页面,添加代码如下:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
int dptId = -1;
Stream stream = null;
if (context.Request.QueryString["dptId"] != null &&
context.Request.QueryString["dptId"] != ""
{
dptId = Convert.ToInt32(context.Request.QueryString["dptId"]);
stream = GetImage(dptId);
}
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0)
{
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
context.Response.End();
}
public Stream GetImage(int dptId)
{
DbManager dbManager = new DbManager();
dbManager.SetDefaultConnectionString();
string strSelect = "select * from pb_depart where DptId='" + dptId + "'";
SqlDataReader reader = dbManager.GetDataReader(strSelect);
byte[] buffer = null;
if (reader.Read())
{
buffer = (byte[])reader["Logo"];
}
MemoryStream stream = null;
try
{
stream = new MemoryStream(buffer);
return stream;
}
catch (ArgumentNullException e)
{
return null;
}
finally
{
reader.Close();
dbManager.Close();
}
}
在web页面中对<asp:Image>进行绑定操作
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "Handler.ashx?PhotoID=" + Eval("dptId" %>' />
或者在.cs文件中对<asp:Image>的ImageUrl属性进行直接赋值,Image1.ImageUrl = "Handler.ashx?dptId=" + dptId.ToString();