Imports System Imports System.Web Imports System.Drawing Imports System.IO Imports System.Drawing.Imaging PublicClass ThumbnailClass Thumbnail Inherits System.Web.UI.Page ProtectedWithEvents Label1 As System.Web.UI.WebControls.Label ProtectedWithEvents Button1 As System.Web.UI.WebControls.Button Web Form Designer Generated Code#Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()>PrivateSub InitializeComponent()Sub InitializeComponent() End Sub PrivateSub Page_Init()Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.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 PrivateSub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load Label1.Text ="<h3>在ASP.NET里轻松实现缩略图</h3>" Button1.Text ="上载并显示缩略图" End Sub PrivateSub Button1_Click()Sub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click Dim MyFileColl As HttpFileCollection = HttpContext.Current.Request.Files Dim MyPostedFile As HttpPostedFile = MyFileColl.Item(0) IfLCase(MyPostedFile.ContentType.ToString()).IndexOf("image") <0Then Response.Write("无效的图形格式。") Exit Sub EndIf GetThumbNail(MyPostedFile.FileName, 100, 100, MyPostedFile.ContentType.ToString(),_ False, MyPostedFile.InputStream) End Sub PrivateFunction GetImageType()Function GetImageType(ByVal strContentType) As System.Drawing.Imaging.ImageFormat SelectCase (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 CaseElse GetImageType = System.Drawing.Imaging.ImageFormat.MemoryBmp EndSelect End Function PrivateSub GetThumbNail()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) EndIf oImg = oImg.GetThumbnailImage(iWidth, iheight, Nothing, (New IntPtr()).Zero) Dim strGuid AsString= (New Guid()).NewGuid().ToString().ToUpper() Dim strFileExt AsString= strFileName.Substring(strFileName.LastIndexOf(".")) '保存到本地 'oImg.Save(Server.MapPath("images") + "" + strGuid + strFileExt, GetImageType(strContentType)) '直接输出url文件 'Response.Redirect("images/" + strGuid + strFileExt) '以下显示在屏幕上 Response.ContentType = strContentType Dim MemStream AsNew MemoryStream() ' 注意:这里如果直接用 oImg.Save(Response.OutputStream, GetImageType(strContentType)) ' 对不同的格式可能会出错,比如Png格式。 oImg.Save(MemStream, GetImageType(strContentType)) MemStream.WriteTo(Response.OutputStream) End Sub End Class