主题:  ASP.NET实现缩略图显示

蓝鲸

职务:版主
等级:5
金币:42.1
发贴:2614
注册:2001/12/20 15:57:57
#12005/2/26 21:58:13
有网友问如何实现缩略图,在这里就说明一下。
下面是一则几个月前用VB写的练习程序

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        Response.Clear()

        If src = "" Then
            sendError()
        Else
            If File.Exists(Server.MapPath(src)) Then
                sendFile()
            Else
                sendError()
            End If
        End If

        Response.End()
    End Sub

    Function newthumbSize(ByVal currentwidth, ByVal currentheight)
        Dim tempMultiplier As Double
        tempMultiplier = 0.5
        Dim newSize As New Size(CInt(currentwidth * tempMultiplier), CInt(currentheight * tempMultiplier))
        Return newSize
    End Function

    Sub sendFile()

        Dim g As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(src))
        Dim thisformat = g.RawFormat

        Dim thumbSize As New Size
        thumbSize = newthumbSize(g.Width, g.Height)

        Dim imgOutput As New Bitmap(g, thumbSize.Width, thumbSize.Height)

        If thisformat.GetType.Equals(System.Drawing.Imaging.ImageFormat.Gif) Then
            Response.ContentType = "image/gif"
        Else
            Response.ContentType = "image/jpeg"
        End If

        imgOutput.Save(Response.OutputStream, thisformat)
        g.Dispose()
        imgOutput.Dispose()

    End Sub

    Sub sendError()
        Dim imgOutput As New Bitmap(120, 120, PixelFormat.Format24bppRgb)
        Dim g As Graphics = Graphics.FromImage(imgOutput)
        g.Clear(Color.Yellow)
        g.DrawString("ERROR!", New Font("verdana", 14, FontStyle.Bold), SystemBrushes.WindowText, New PointF(2, 2))

        Response.ContentType = "image/gif"

        imgOutput.Save(Response.OutputStream, ImageFormat.Gif)

        g.Dispose()
        imgOutput.Dispose()

    End Sub

End Class


调用时用:
Image1.ImageUrl = "053.aspx"


非常大鱼