前一篇的縮圖雖然成功了,但品質有夠差....會在邊緣處看到格子狀
所以又上網找了一下,然後發現要用 System.Drawing.Graphics 去做
因為它有提供 CompositingQuality 的圖片品質設定

相關資料可以看這裡 :
http://msdn.microsoft.com/zh-tw/library/system.drawing.graphics_properties(VS.90).aspx

但老實講我長這麼大看 MSDN 還是會看到頭痛,我一直覺得它是翻譯給外星人看的

順便附上找到的一些資料
http://ursecretgarden.blogspot.com/2009/11/asp-net-resize-images-online.html
http://www.glennjones.net/Post/799/Highqualitydynamicallyresizedimageswithnet.htm

然後程式碼變成下面這樣

Imports System
Imports System.Web
Imports System.IO
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 = "Y:\Brand333_Web\fileupload\resize\"
        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 fileExists As Boolean
            fileExists = My.Computer.FileSystem.FileExists(End_path & filename & "_" & w & "_" & h & ".jpg")
            If fileExists = False Then
                Dim sFileStream As FileStream = File.OpenRead("Y:\Brand333_Web\" & Replace(files, "/", "\"))
                Dim tFileStream As FileStream = File.Create(End_path & filename & "_" & w & "_" & h & ".jpg")           
                      
                ResizeImage(0.1, sFileStream, tFileStream, w, h)  
                sFileStream.Close() 
                tFileStream.Close()
                Dim fstr As String = End_path & filename & "_" & w & "_" & h & ".jpg"
                context.Response.ContentType = "image/jpeg"
                context.Response.WriteFile(fstr)
            Else
                'thumb.Save(context.Response.OutputStream, img.RawFormat)
                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
    
    
    Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream, ByVal w As Integer, ByVal h As Integer)
        Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(fromStream)
        Dim newWidth As Integer = CType((image.Width * scaleFactor), Integer)
        Dim newHeight As Integer = CType((image.Height * scaleFactor), Integer)
        Dim thumbnailBitmap As Bitmap = New Bitmap(w, h)
        Dim thumbnailGraph As Graphics = Graphics.FromImage(thumbnailBitmap)
        '要高品質就是下面幾行
        thumbnailGraph.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
        thumbnailGraph.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        thumbnailGraph.InterpolationMode = Drawing2D.InterpolationMode.High
        
        Dim imageRectangle As Rectangle = New Rectangle(0, 0, w, h)
        thumbnailGraph.DrawImage(image, imageRectangle)
        thumbnailBitmap.Save(toStream, image.RawFormat)
        thumbnailGraph.Dispose()
        thumbnailBitmap.Dispose()
        image.Dispose()
    End Sub
End Class

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 小雕 的頭像
    小雕

    小雕雕的家

    小雕 發表在 痞客邦 留言(0) 人氣()