首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 輕鬆實現縮圖生成:Image.GetThumbnailImage、上傳即時處理與記憶體串流輸出完整範例

     編輯:關於ASP.NET

.NET Framework 內建 Image.GetThumbnailImage 讓縮圖生成無需第三方元件。本文提供完整上傳 → 生成縮圖 → 串流輸出的 ASP.NET WebForms 範例。

前端頁面 (Thumbnail.aspx)

<%@ Page Language="VB" AutoEventWireup="false" Codebehind="Thumbnail.aspx.vb" Inherits="aspxWeb.Thumbnail" %>
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
  <input type="file" name="file" /><br><br>
  <asp:Button ID="Button1" runat="server" Text="上傳並顯示縮圖" OnClick="Button1_Click" />
</form>
<asp:Label ID="Label1" runat="server" />

後端核心邏輯 (Thumbnail.aspx.vb)

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Public Class Thumbnail
    Inherits System.Web.UI.Page

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim postedFile As HttpPostedFile = Request.Files(0)
        If postedFile.ContentLength = 0 OrElse Not postedFile.ContentType.StartsWith("image/") Then
            Response.Write("請選擇有效圖片檔案")
            Return
        End If

        ' 直接從上傳串流生成縮圖並輸出
        GetThumbnail(postedFile.InputStream, postedFile.ContentType, 100, 100)
    End Sub

    Private Sub GetThumbnail(ByVal imgStream As Stream, ByVal contentType As String, ByVal width As Int32, ByVal height As Int32)
        Using original As Image = Image.FromStream(imgStream)
            ' GetThumbnailImage 內部使用內嵌縮圖 (若有) 或快速縮放
            Using thumb As Image = original.GetThumbnailImage(width, height, Nothing, IntPtr.Zero)
                Response.Clear()
                Response.ContentType = contentType
                Using ms As New MemoryStream()
                    ' 注意:直接 Save(Response.OutputStream) 對某些格式 (如 PNG) 可能報錯
                    ' 先存 MemoryStream 再 WriteTo 較穩健
                    thumb.Save(ms, GetImageFormat(contentType))
                    ms.WriteTo(Response.OutputStream)
                End Using
                Response.End()
            End Using
        End Using
    End Sub

    Private Function GetImageFormat(ByVal contentType As String) As ImageFormat
        Select Case contentType.ToLower()
            Case "image/pjpeg", "image/jpeg" : Return ImageFormat.Jpeg
            Case "image/gif" : Return ImageFormat.Gif
            Case "image/bmp" : Return ImageFormat.Bmp
            Case "image/png" : Return ImageFormat.Png
            Case "image/tiff" : Return ImageFormat.Tiff
            Case Else : Return ImageFormat.Jpeg
        End Select
    End Function
End Class

關鍵技術點

  • enctype="multipart/form-data":表單必須設定,否則 Request.Files 為空。
  • Image.FromStream:直接從上傳串流讀取,避免暫存檔 I/O。
  • GetThumbnailImage(w, h, callback, IntPtr.Zero):
    callback 為過時參數 (需傳 Nothing),IntPtr.Zero 表示無回調。
  • MemoryStream 中轉:解決 Image.Save(Response.OutputStream) 對 PNG/GIF 可能產生的 GDI+ 一般性錯誤。
  • Response.End():終止頁面生命週期,確保純圖片回應無 HTML 污染。

進階:高品質縮圖 (InterpolationMode.HighQualityBicubic)

Using bmp As New Bitmap(width, height)
    Using g As Graphics = Graphics.FromImage(bmp)
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.DrawImage(original, New Rectangle(0, 0, width, height))
    End Using
    ' bmp 為高品質縮圖
End Using

GetThumbnailImage 速度快但品質一般;高品質需求請用 Graphics.DrawImage 搭配雙三次插值。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved