• λ我爱Aspx >> Asp.Net >> 在ASP.NET里轻松实现缩略图
  • 在ASP.NET里轻松实现缩略图

  • :aspxer  Դ:网络  :2007-4-25 21:17:34  ؼ:.net,asp.net,asp
  • 在ASP.NET里轻松实现缩略图 作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年5月23日 8点26分35秒


    以前,在页面上实现缩略图必须借助第三方组件。现在,有了.NET,就可以很轻松地实现缩略图。下面就是实现缩略图的例子。

    查看例子

    代码如下:Thumbnail.aspx

    <%@ page language="vb" autoeventwireup="false" codebehind="thumbnail.aspx.vb" inherits="aspxweb.thumbnail" %> <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>在ASP.NET里轻松实现缩略图</title> <meta content="microsoft visual studio.net 7.0" name="generator"> <meta content="visual basic 7.0" name="code_language"> <meta content="javascript" name="vs_defaultclientscript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema"> </head> <body ms_positioning="gridlayout"> </asp:label> <form id="form1" method="post" runat="server" enctype="multipart/form-data"> <input type="file" name="file" width="600"></p> <p> </asp:button> </form> </body> </html>

    后代码:Thumbnail.aspx.vb

    Imports System Imports System.Web Imports System.Drawing Imports System.IO Imports System.Drawing.Imaging Public Class Thumbnail Inherits System.Web.UI.Page Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Button1 As System.Web.UI.WebControls.Button #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Label1.Text = "<h3>在ASP.NET里轻松实现缩略图</h3>" Button1.Text = "上载并显示缩略图" End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim MyFileColl As HttpFileCollection = HttpContext.Current.Request.Files Dim MyPostedFile As HttpPostedFile = MyFileColl.Item(0) If LCase(MyPostedFile.ContentType.ToString()).IndexOf("image") < 0 then response.write("无效的图形格式。") exit sub end if getthumbnail(mypostedfile.filename, 100, 100, mypostedfile.contenttype.tostring(),_ false, mypostedfile.inputstream) end sub private function getimagetype(byval strcontenttype) as system.drawing.imaging.imageformat select case (strcontenttype.tostring().tolower()) case "image/pjpeg" getimagetype = system.drawing.imaging.imageformat.jpeg case "image/gif" getimagetype = system.drawing.imaging.imageformat.gif case "image/bmp" getimagetype = system.drawing.imaging.imageformat.bmp case "image/tiff" getimagetype = system.drawing.imaging.imageformat.tiff case "image/x-icon" getimagetype = system.drawing.imaging.imageformat.icon case "image/x-png" getimagetype = system.drawing.imaging.imageformat.png case "image/x-emf" getimagetype = system.drawing.imaging.imageformat.emf case "image/x-exif" getimagetype = system.drawing.imaging.imageformat.exif case "image/x-wmf" getimagetype = system.drawing.imaging.imageformat.wmf case else getimagetype = system.drawing.imaging.imageformat.memorybmp end select end function private sub getthumbnail(byval strfilename, byval iwidth, byval iheight, byval strcontenttype, _ byval blngetfromfile, byval imgstream) dim oimg as image if blngetfromfile then oimg = oimg.fromfile(strfilename) else oimg = oimg.fromstream(imgstream) end if oimg = oimg.getthumbnailimage(iwidth, iheight, nothing, (new intptr()).zero) dim strguid as string = (new guid()).newguid().tostring().toupper() dim strfileext as string = strfilename.substring(strfilename.lastindexof(".")) '保存到本地 'oimg.save(server.mappath("images") + "\" + strguid + strfileext, getimagetype(strcontenttype)) '直接输出url文件 'response.redirect("images/" + strguid + strfileext) '以下显示在屏幕上 response.contenttype = strcontenttype dim memstream as new memorystream() ' 注意:这里如果直接用 oimg.save(response.outputstream, getimagetype(strcontenttype)) ' 对不同的格式可能会出错,比如png格式。 oimg.save(memstream, getimagetype(strcontenttype)) memstream.writeto(response.outputstream) end sub end class

    Ҷƪл˵?
  • һƪ为DataGrid添加CheckBox控件
    һƪ为DataGrid添加自动编号功能