圖片縮圖好不容易搞定,但是找不到該如何截圖 (把某高度下的圖片截斷)...而且等比縮也還沒寫...
使用方式....
<img src="RsizeImg.ashx?file=/images/Ooops.jpg&w=80&h=20" />
功用....
如果原圖還沒縮圖就產生縮圖並輸出,如果已經有縮圖就直接輸出
code 如下 :
Imports System
Imports System.Web
Imports System.Drawing
Imports System.Drawing.Imaging
Public Class RsizeImg : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "image/jpeg"
context.Response.BufferOutput = True
'縮圖輸出位置
Dim End_path As String = "D:\Web\Brand333_Web\Images\"
Dim files As String = context.Request("file")
Dim w As Integer = 0
Dim h As Integer = 0
If context.Request("w") IsNot Nothing Then
w = context.Request("w")
End If
If context.Request("h") IsNot Nothing Then
h = context.Request("h")
End If
If w <> 0 And h <> 0 Then
'拆檔名
Dim filename As String = mid(files, InStrRev(files, "/") + 1
filename = Mid(filename, 1, InStr(filename, ".") - 1)
Dim img As Image = Image.FromFile("D:\Web\Brand333_Web" & Replace(files, "/", "\"))
Dim mycallback As Image.GetThumbnailImageAbort = New Image.GetThumbnailImageAbort(AddressOf ThumbNailCallBack)
Dim thumb As Image = New Bitmap(img, w, h)
Dim fileExists As Boolean
'檔名後面 + 上寬高
fileExists = My.Computer.FileSystem.FileExists("D:\Web\Brand333_Web\Images\" & filename & "_" & w & "_" & h & ".jpg")
If fileExists = False Then
thumb.Save(End_path & filename & "_" & w & "_" & h & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Dim fstr As String = End_path & filename & "_" & w & "_" & h & ".jpg"
context.Response.ContentType = "image/jpeg"
context.Response.WriteFile(fstr)
Else
Dim fstr As String = End_path & filename & "_" & w & "_" & h & ".jpg"
context.Response.ContentType = "image/jpeg"
context.Response.WriteFile(fstr)
End If
End If
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Function ThumbNailCallBack() As Boolean
Return False
End Function
End Class