11/03/2009

ASP .NET - 如何線上產生高品質的縮圖

ASP .NET的線上縮圖程式,一般都是使用Image.GetThumbnailImage的方式來實作。使用Image.GetThumbnailImage雖然是很簡便,但其縮圖的品質有時真是慘不忍睹,尤其是對高畫質﹝High Quailty﹞的原圖來作縮圖。
因此,要產生高品質的縮圖,我們必須捨棄Image.GetThumbnailImage的方式,自行對圖片來作縮圖處理:


<%@ Page Language="VB" %>


<script runat="server">


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

'----------------------------------------------------------------

Dim sFileName As String = Server.MapPath("SANY0722.jpg")

Dim tFileName As String = Server.MapPath("SANY0722_thumb.jpg")

'----------------------------------------------------------------

' Delete the file if it exists.

'----------------------------------------------------------------

If File.Exists(tFileName) Then

File.Delete(tFileName)

End If

'----------------------------------------------------------------

Dim sFileStream As FileStream = File.OpenRead(sFileName)

Dim tFileStream As FileStream = File.Create(tFileName)

'----------------------------------------------------------------

ResizeImage(0.1, sFileStream, tFileStream)

'----------------------------------------------------------------

sFileStream.Close()

tFileStream.Close()

'----------------------------------------------------------------

End Sub


Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream As Stream)

'----------------------------------------------------------------

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(newWidth, newHeight)

Dim thumbnailGraph As Graphics = Graphics.FromImage(thumbnailBitmap)

'----------------------------------------------------------------

thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality

thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality

thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic

'----------------------------------------------------------------

Dim imageRectangle As Rectangle = New Rectangle(0, 0, newWidth, newHeight)

'----------------------------------------------------------------

thumbnailGraph.DrawImage(image, imageRectangle)

thumbnailBitmap.Save(toStream, image.RawFormat)

'----------------------------------------------------------------

thumbnailGraph.Dispose

thumbnailBitmap.Dispose

image.Dispose

'----------------------------------------------------------------

End Sub


</script>


以下是利用Image.GetThumbnailImage和自行縮圖處理的範例比較。左圖是Image.GetThumbnailImage所產生的縮圖;右圖是自行處理的縮圖﹝原圖尺寸為2910x4370;縮圖比例為10%﹞:
使用Image.GetThumbnailImage所產生的縮圖自行處理的縮圖

網路上流傳:使用兩次Image.GetThumbnailImage來縮圖,取其第二次的縮圖其品質會比較好,以下我們也就二次縮圖的品質來作一下測試:


<%@ Page Language="VB" %>


<script runat="server">


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

'----------------------------------------------------------------

Dim sFileName As String = Server.MapPath("SANY0722.jpg")

Dim tFileName_1st As String = Server.MapPath("SANY0722_thumb_1st.jpg")

Dim tFileName_2nd As String = Server.MapPath("SANY0722_thumb_2nd.jpg")

'----------------------------------------------------------------

' Delete the file if it exists.

'----------------------------------------------------------------

If File.Exists(tFileName_1st) Then

File.Delete(tFileName_1st)

End If

'----------------------------------------------------------------

If File.Exists(tFileName_2nd) Then

File.Delete(tFileName_2nd)

End If

'----------------------------------------------------------------

Dim sFileStream As FileStream = File.OpenRead(sFileName)

Dim tFileStream_1st As FileStream = File.Create(tFileName_1st)

Dim tFileStream_2nd As FileStream = File.Create(tFileName_2nd)

'----------------------------------------------------------------

ResizeImage(0.1, sFileStream, tFileStream_1st, tFileStream_2nd)

'----------------------------------------------------------------

sFileStream.Close()

tFileStream_1st.Close()

tFileStream_2nd.Close()

'----------------------------------------------------------------

End Sub


Public Sub ResizeImage(ByVal scaleFactor As Double, ByVal fromStream As Stream, ByVal toStream_1st As Stream, ByVal toStream_2nd As Stream)

'----------------------------------------------------------------

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 thumbnail_1st As System.Drawing.Image = image.GetThumbnailImage(newWidth, newHeight, Nothing, IntPtr.Zero)

Dim thumbnail_2nd As System.Drawing.Image = image.GetThumbnailImage(newWidth, newHeight, Nothing, IntPtr.Zero)

'----------------------------------------------------------------

thumbnail_1st.Save(toStream_1st, image.RawFormat)

thumbnail_2nd.Save(toStream_2nd, image.RawFormat)

'----------------------------------------------------------------

thumbnail_1st.Dispose

thumbnail_2nd.Dispose

image.Dispose

'----------------------------------------------------------------

End Sub


</script>


左圖是第一次使用Image.GetThumbnailImage所產生的縮圖;右圖是第二次使用Image.GetThumbnailImage所產生的縮圖:
第一次使用Image.GetThumbnailImage所產生的縮圖第二次使用Image.GetThumbnailImage所產生的縮圖

二次使用Image.GetThumbnailImage後的縮圖品質,其實已是可接受的範圍,但與我們自行處理的縮圖還是有點差距。左圖是二次使用Image.GetThumbnailImage所產生的縮圖;右圖是自行處理的縮圖,眼尖的您是否看出有所不同:
第二次使用Image.GetThumbnailImage所產生的縮圖自行處理的縮圖



添加到收藏夾 / 分享




1 則留言: