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. 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 = "
在ASP.NET里轻松实现缩略图
" 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 Ҷƪл˵?