1.建立一個 ashx ,我是建立一個名稱為 Thumbnail.ashx
2.以下為 Thumbnail.ashx 程式碼
<%@ WebHandler Language="VB" Class="Thumbnail" %> Imports System Imports System.Web Imports System.Drawing Imports System.Drawing.Image Public Class Thumbnail : Implements IHttpHandler Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest context.Response.ContentType = "image/jpeg" '檔案名稱 Dim file As String = context.Request("file") '縮圖大小 Dim max As Integer = context.Request("max") '取得實際圖片 Dim img As Image = Image.FromFile(context.Server.MapPath(file)) Dim w As Double '寬 Dim h As Double '長 '照比例縮小 If img.Width > img.Height Then w = max h = img.Height * (max / img.Width) Else h = max w = img.Width * (max / img.Height) End If Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) Dim thumb As Image = New Bitmap(img, Convert.ToInt32(w), Convert.ToInt32(h)) '將圖形儲存到頁面輸出串流中,並指定輸出格式 thumb.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg) thumb.Dispose() End Sub Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return False End Get End Property Public Function ThumbnailCallback() As Boolean return false End Function End Class
3.以下為使用縮圖頁面的程式碼,請注意<img src="xxxx">的部份
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>
4.在src=" "裡面指向我們建立好的ashx檔,並下了參數 max 跟file
max 為縮圖的大小
file 為伺服器檔案的路徑(只能為相對路徑)
指定好後,就可以使用縮圖了
下一篇文章來教學使用縮圖及浮水印
全站熱搜
留言列表