如何把存储在数据库中的图片根据自己的需要的大小显示出来
程序员文章站
2022-11-18 12:58:50
文件1:showimage.x.csnamespace imageresizing {public class maindisplay : system.web.ui.page {...
文件1:showimage.x.cs
namespace imageresizing {
public class maindisplay : system.web.ui.page {
public void page_load(system.object sender, system.eventargs e) {
try {
system.int32 _imgid = system.convert.toint32(request.querystring["imgid"]);
system.int32 _height = system.convert.toint32(request.querystring["height"]);
system.int32 _width = system.convert.toint32(request.querystring["width"]);
system.data.sqlclient.sqlconnection con = new system.data.sqlclient.sqlconnection( "server=localhost;database=northwind;trusted_connection=true" );
system.string sqlcmd = "select * from images where imageid = @imageid";
system.data.sqlclient.sqlcommand sqlcmdobj = new system.data.sqlclient.sqlcommand( sqlcmd, con );
sqlcmdobj.parameters.add("@imageid", system.data.sqldbtype.int).value = _imgid;
con.open();
system.data.sqlclient.sqldatareader sqlreader = sqlcmdobj.executereader();
sqlreader.read();
system.web.httpcontext.current.response.contenttype = "image/pjpeg";
system.drawing.image _image = system.drawing.image.fromstream( new system.io.memorystream( (byte[])sqlreader["image"] ) );
system.drawing.image _newimage = _image.getthumbnailimage( _width, _height, null, new system.intptr());
_newimage.save( system.web.httpcontext.current.response.outputstream, system.drawing.imaging.imageformat.jpeg );
} catch (system.exception ex) {
system.web.httpcontext.current.trace.write(ex.message.tostring());
}
}
}
}
文件2:显示图片之用,把querystring传入
<html>
<body>
<img src="showimage.aspx?imgid=202&height=150&width=150">
</body>
</html>
namespace imageresizing {
public class maindisplay : system.web.ui.page {
public void page_load(system.object sender, system.eventargs e) {
try {
system.int32 _imgid = system.convert.toint32(request.querystring["imgid"]);
system.int32 _height = system.convert.toint32(request.querystring["height"]);
system.int32 _width = system.convert.toint32(request.querystring["width"]);
system.data.sqlclient.sqlconnection con = new system.data.sqlclient.sqlconnection( "server=localhost;database=northwind;trusted_connection=true" );
system.string sqlcmd = "select * from images where imageid = @imageid";
system.data.sqlclient.sqlcommand sqlcmdobj = new system.data.sqlclient.sqlcommand( sqlcmd, con );
sqlcmdobj.parameters.add("@imageid", system.data.sqldbtype.int).value = _imgid;
con.open();
system.data.sqlclient.sqldatareader sqlreader = sqlcmdobj.executereader();
sqlreader.read();
system.web.httpcontext.current.response.contenttype = "image/pjpeg";
system.drawing.image _image = system.drawing.image.fromstream( new system.io.memorystream( (byte[])sqlreader["image"] ) );
system.drawing.image _newimage = _image.getthumbnailimage( _width, _height, null, new system.intptr());
_newimage.save( system.web.httpcontext.current.response.outputstream, system.drawing.imaging.imageformat.jpeg );
} catch (system.exception ex) {
system.web.httpcontext.current.trace.write(ex.message.tostring());
}
}
}
}
文件2:显示图片之用,把querystring传入
<html>
<body>
<img src="showimage.aspx?imgid=202&height=150&width=150">
</body>
</html>