在ASP.NET裡輕鬆實現縮圖 (轉)

amyz發表於2007-08-15
在ASP.NET裡輕鬆實現縮圖 (轉)[@more@]

在裡輕鬆實現縮圖

playground.com/">

以前,在頁面上實現縮圖必須藉助第三方。現在,有了.NET,就可以很輕鬆地實現縮圖。下面就是實現縮圖的例子。

" target=_blank>檢視例子

程式碼如下:Thumbnail.aspx

.Thumbnail&quot; %&gt; w3c//DTD HTML 4.0 Transitional//EN&quot;&gt; <title>在裡輕鬆實現縮圖</title> <meta content="a href=" tag-126-1.html="">microsoft .NET 7.0&quot; name=&quot;GENERATOR&quot;&gt; <meta content="a href=" tag-50-1.html=""> 7.0&quot; name=&quot;CODE_LANGUAGE&quot;&gt; <meta content="a href=" tag-133-1.html="">script&quot; name=&quot;vs_defaultClientScript&quot;&gt; <meta content=" href=" tag-340-1.html="">intellisense/&quot; name=&quot;vs_targetSchema&quot;&gt; id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot; enctype=&quot;multipart/form-data&quot;&gt; <input type="file" name="file" width="600"><br><br> gerStepThrough()&gt; Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System., 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 = &quot;<h3>在ASP.NET裡輕鬆實現縮圖</h3>&quot; Button1.Text = &quot;上載並顯示縮圖&quot; 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(&quot;image&quot;) &lt;0 then exit sub end if end sub private function as href=&quot;tag-73-1.html&quot;&gt;select Case (strContentType.ToString().ToLower()) Case &quot;image/pjpeg&quot; GetImageType = System.Drawing.Imaging.ImageFormat.Jpeg Case &quot;image/gif&quot; GetImageType = System.Drawing.Imaging.ImageFormat.Gif Case &quot;image/bmp&quot; GetImageType = System.Drawing.Imaging.ImageFormat.Bmp Case &quot;image/tiff&quot; GetImageType = System.Drawing.Imaging.ImageFormat.Tiff Case &quot;image/x-icon&quot; GetImageType = System.Drawing.Imaging.ImageFormat.Icon Case &quot;image/x-png&quot; GetImageType = System.Drawing.Imaging.ImageFormat.Png Case &quot;image/x-emf&quot; GetImageType = System.Drawing.Imaging.ImageFormat.Emf Case &quot;image/x-exif&quot; GetImageType = System.Drawing.Imaging.ImageFormat.Exif Case &quot;image/x-wmf&quot; 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(&quot;.&quot;)) '儲存到本地 'oImg.Save(Server.MapPath(&quot;images&quot;) + &quot;&quot; + strGuid + strFileExt, GetImageType(strContentType)) '直接輸出url 'Response.Redirect(&quot;images/&quot; + 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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-957924/,如需轉載,請註明出處,否則將追究法律責任。

相關文章