如何把存储在数据库中的图片根据自己的需要的大小显示出来。【转】
2024-07-21 02:16:45
供稿:网友
文件1:showimage.aspx.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>
相关链接:http://www.aspnextgen.com/tutorials.aspx?tutorialid=176 国内最大的酷站演示中心!