Public Class GetThumbnail Inherits System.Web.UI.Page #Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub '注意: 以下占位符声明是 Web 窗体设计器所必需的。 '不要删除或移动它。 Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的 '不要使用代码编辑器修改它。 InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 '获取几个参数,用以生成缩略图片 '设置一个默认参数生成BitMap Response.Clear() Try Dim originalFileName As String = Server.MapPath(Request("fn")) Dim thumbnailWidth As Integer = Request("tw") Dim thumbnailHeight As Integer = Request("th") Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(originalFileName) Dim thisFormat As System.Drawing.Imaging.ImageFormat = img.RawFormat Dim newSize As System.Drawing.Size = Me.GetNewSize(thumbnailWidth, thumbnailHeight, img.Width, img.Height) Dim outBmp As New System.Drawing.Bitmap(thumbnailWidth, thumbnailHeight) Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(outBmp) '设置画布的描绘质量 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic g.Clear(System.Drawing.Color.FromArgb(255, 249, 255, 240)) g.DrawImage(img, New System.Drawing.Rectangle((thumbnailWidth - newSize.Width) /2, (thumbnailHeight - newSize.Height) /2, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, System.Drawing.GraphicsUnit.Pixel) g.Dispose() If thisFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif) Then Response.ContentType ="image/gif" Else Response.ContentType ="image/jpeg" End If '设置压缩质量 Dim encoderParams As New System.Drawing.Imaging.EncoderParameters Dim quality(0) As Long quality(0) =100 Dim encoderParam As New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality) encoderParams.Param(0) = encoderParam '获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。 Dim arrayICI() As System.Drawing.Imaging.ImageCodecInfo = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders() Dim jpegICI As System.Drawing.Imaging.ImageCodecInfo = Nothing For fwd As Integer =0 To arrayICI.Length -1 If arrayICI(fwd).FormatDescription.Equals("JPEG") Then jpegICI = arrayICI(fwd) Exit For End If Next If Not jpegICI Is Nothing Then outBmp.Save(Response.OutputStream, jpegICI, encoderParams) Else outBmp.Save(Response.OutputStream, thisFormat) End If Catch ex As Exception End Try End Sub Function GetNewSize(ByVal maxWidth As Integer, ByVal maxHeight As Integer, ByVal width As Integer, ByVal height As Integer) As System.Drawing.Size Dim w As Double =0.0 Dim h As Double =0.0 Dim sw As Double = Convert.ToDouble(width) Dim sh As Double = Convert.ToDouble(height) Dim mw As Double = Convert.ToDouble(maxWidth) Dim mh As Double = Convert.ToDouble(maxHeight) If sw < mw And sh < mh Then w = sw h = sh ElseIf (sw / sh) > (mw / mh) Then w = maxWidth h = (w * sh) / sw Else h = maxHeight w = (h * sw) / sh End If Return New System.Drawing.Size(Convert.ToInt32(w), Convert.ToInt32(h)) End Function End Class