網路上流傳:使用兩次Image.GetThumbnailImage來縮圖,取其第二次的縮圖其品質會比較好
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
沒有留言:
張貼留言