asp.net(c#)实现从sqlserver存取二进制图片的代码
程序员文章站
2024-03-07 18:56:09
下面说说主要实现思路: 1、存取图片 (1)、将图片文件转换为二进制并直接存进sql server 复制代码 代码如下: //uploadhelper.cs /// <...
下面说说主要实现思路:
1、存取图片
(1)、将图片文件转换为二进制并直接存进sql server
//uploadhelper.cs
/// <summary>
/// 将图片转化为长二进制
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static byte[] setimgtobyte(string imgpath)
{
filestream file = new filestream(imgpath, filemode.open, fileaccess.read);
byte[] bytedata = new byte[file.length];
file.read(bytedata, 0, bytedata.length);
file.close();
return bytedata;
}
/// <summary>
/// 将转换成二进制码的图片保存到数据库中
/// </summary>
public static bool saveemployeeimg2db(employee model, string path)
{
try
{
byte[] imgbytes = setimgtobyte(path);
model.photo = imgbytes;
bool flag=employeeservice.saveemployeephoto(model); //employeeservice是公司内部的库调用,插入或者更新照片,这里不透露细节
return flag;
}
catch (exception ex)
{
throw ex;
}
}
(2)、在网页中上传图片
/// <summary>
/// 上传图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnupload_click(object sender, eventargs e)
{
string serverpath = server.mappath("~/images/");
if (this.fuphoto.hasfile) //fuphoto是fileupload控件
{
string filename = this.fuphoto.postedfile.filename;
fileinfo fi = new fileinfo(filename);
string mimetype = this.fuphoto.postedfile.contenttype.tolower();
if (mimetype.indexof("image") < 0)
{
//("上传的照片格式不对");
}
else if(fi.length > 2* 1024 * 1024)
{
//图片大于2m,重新处理
}
else
{
string savefilepath = serverpath + datetime.now.tostring("yyyymmddhhmmss") + filename;
try
{
//先存图片到服务器
this.fuphoto.postedfile.saveas(savefilepath);
//转成二进制
employee model = new employee(int.parse(id)); //id是employeeid,这里是模拟字段
bool flag = uploadhelper.saveemployeeimg2db(model, savefilepath);
}
catch
{
//("照片上传失败");
}
finally
{
//最后删掉该图片
if (system.io.file.exists(savefilepath))
{
system.io.file.delete(savefilepath);
}
}
}
}
else
{
//("全选择要上传的照片");
}
}
(3)、从数据库取出照片(返回格式image)
//uploadhelper.cs
/// <summary>
/// 将二进制转化为图片image
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static system.drawing.image getimgfrombyte(employee model)
{
system.drawing.image img = null;
try
{
stream stream = new memorystream(model.photo);
img = system.drawing.image.fromstream(stream,false);
}
catch
{
img = null;
}
return img;
}
上面的这个方法取出来之后,如果在winform下,直接给一个picturebox的image属性赋值就可以了。可是web下没有这么强大的控件,所以,就有了下面的步骤。
2、直接在网页中以流的形式显示图片
(1)、生成图片流页面(imghelper .aspx)
这个页面的设计页面什么也没有,类文件如下:
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.collections.generic;
using system.io;
/// <summary>
/// 图片辅助类
/// </summary>
public partial class imghelper : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!string.isnullorempty(request["employeeid"])) //需要显示照片的页面传递的员工id
{
int employeeid = int.parse(request["employeeid"]);
employee model = //employeeservice.getemployeebycondition(new employee(employeeid))[0] as employee; //内部函数 查找一个员工 不透漏细节
try
{
byte[] byteimg = model.photo;
stream stream = new memorystream(byteimg);
system.drawing.bitmap img =(system.drawing.bitmap) system.drawing.bitmap.fromstream(stream, false); //转换成bitmap
response.buffer = false;
response.contenttype = "image/jpg";
response.addheader("content-disposition", "attachment;filename=photo.jpg");//照片名称叫photo.jpg
response.binarywrite(byteimg);//写入二进制流
response.end();
}
catch
{
response.end();
}
}
}
}
(2)、显示照片的页面调用imghelper .aspx
在页面加载的时候,给图片控件赋值如下:
this.imgphoto.imageurl = "/imghelper.aspx?employeeid="+tmpemployee.id.tostring(); //imgphoto是图片控件
总体来说,一存一取,对于winform是很方便的,但是对于webform,我们需要稍微有一个转化的思路。如果有牛人写出像winform下那种直接绑定image对象的控件更好了。上面代码测试通过,希望对你有帮助。
1、存取图片
(1)、将图片文件转换为二进制并直接存进sql server
复制代码 代码如下:
//uploadhelper.cs
/// <summary>
/// 将图片转化为长二进制
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static byte[] setimgtobyte(string imgpath)
{
filestream file = new filestream(imgpath, filemode.open, fileaccess.read);
byte[] bytedata = new byte[file.length];
file.read(bytedata, 0, bytedata.length);
file.close();
return bytedata;
}
/// <summary>
/// 将转换成二进制码的图片保存到数据库中
/// </summary>
public static bool saveemployeeimg2db(employee model, string path)
{
try
{
byte[] imgbytes = setimgtobyte(path);
model.photo = imgbytes;
bool flag=employeeservice.saveemployeephoto(model); //employeeservice是公司内部的库调用,插入或者更新照片,这里不透露细节
return flag;
}
catch (exception ex)
{
throw ex;
}
}
(2)、在网页中上传图片
复制代码 代码如下:
/// <summary>
/// 上传图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnupload_click(object sender, eventargs e)
{
string serverpath = server.mappath("~/images/");
if (this.fuphoto.hasfile) //fuphoto是fileupload控件
{
string filename = this.fuphoto.postedfile.filename;
fileinfo fi = new fileinfo(filename);
string mimetype = this.fuphoto.postedfile.contenttype.tolower();
if (mimetype.indexof("image") < 0)
{
//("上传的照片格式不对");
}
else if(fi.length > 2* 1024 * 1024)
{
//图片大于2m,重新处理
}
else
{
string savefilepath = serverpath + datetime.now.tostring("yyyymmddhhmmss") + filename;
try
{
//先存图片到服务器
this.fuphoto.postedfile.saveas(savefilepath);
//转成二进制
employee model = new employee(int.parse(id)); //id是employeeid,这里是模拟字段
bool flag = uploadhelper.saveemployeeimg2db(model, savefilepath);
}
catch
{
//("照片上传失败");
}
finally
{
//最后删掉该图片
if (system.io.file.exists(savefilepath))
{
system.io.file.delete(savefilepath);
}
}
}
}
else
{
//("全选择要上传的照片");
}
}
(3)、从数据库取出照片(返回格式image)
复制代码 代码如下:
//uploadhelper.cs
/// <summary>
/// 将二进制转化为图片image
/// </summary>
/// <param name="photopath"></param>
/// <returns></returns>
public static system.drawing.image getimgfrombyte(employee model)
{
system.drawing.image img = null;
try
{
stream stream = new memorystream(model.photo);
img = system.drawing.image.fromstream(stream,false);
}
catch
{
img = null;
}
return img;
}
上面的这个方法取出来之后,如果在winform下,直接给一个picturebox的image属性赋值就可以了。可是web下没有这么强大的控件,所以,就有了下面的步骤。
2、直接在网页中以流的形式显示图片
(1)、生成图片流页面(imghelper .aspx)
这个页面的设计页面什么也没有,类文件如下:
复制代码 代码如下:
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.collections.generic;
using system.io;
/// <summary>
/// 图片辅助类
/// </summary>
public partial class imghelper : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!string.isnullorempty(request["employeeid"])) //需要显示照片的页面传递的员工id
{
int employeeid = int.parse(request["employeeid"]);
employee model = //employeeservice.getemployeebycondition(new employee(employeeid))[0] as employee; //内部函数 查找一个员工 不透漏细节
try
{
byte[] byteimg = model.photo;
stream stream = new memorystream(byteimg);
system.drawing.bitmap img =(system.drawing.bitmap) system.drawing.bitmap.fromstream(stream, false); //转换成bitmap
response.buffer = false;
response.contenttype = "image/jpg";
response.addheader("content-disposition", "attachment;filename=photo.jpg");//照片名称叫photo.jpg
response.binarywrite(byteimg);//写入二进制流
response.end();
}
catch
{
response.end();
}
}
}
}
(2)、显示照片的页面调用imghelper .aspx
在页面加载的时候,给图片控件赋值如下:
复制代码 代码如下:
this.imgphoto.imageurl = "/imghelper.aspx?employeeid="+tmpemployee.id.tostring(); //imgphoto是图片控件
总体来说,一存一取,对于winform是很方便的,但是对于webform,我们需要稍微有一个转化的思路。如果有牛人写出像winform下那种直接绑定image对象的控件更好了。上面代码测试通过,希望对你有帮助。