asp.net生成高质量缩略图(VB.net)

来源:互联网 发布:rocketdock类似软件 编辑:程序博客网 时间:2024/05/22 00:09

最近给公司做一个固定资产管理的小程序,要显示固定资产的照片,显示原图的话会因图片太大而影响速度,于是就改为显示缩略图,下面的二种方法中第二种方法生成的图片很小且质量较高,原图为900K大小,第一种生成后有300K,第二种生成后只有十多K,有兴趣的可以看看。

方法一:

           img_tp.ImageUrl = ""
          Dim callb As System.Drawing.Image.GetThumbnailImageAbort = Nothing
          Dim myimage As System.Drawing.Image
          Dim newimage As System.Drawing.Image
          NewImage = MyImage.GetThumbnailImage(800, 1000, callb, new System.IntPtr()); 
          myimage = System.Drawing.Image.FromFile(Server.MapPath(xh))
          newimage = myimage.GetThumbnailImage(320, 240, callb, New System.IntPtr())
          newimage.Save(Server.MapPath(xh + "_.jpg"))
          img_tp.ImageUrl = xh + "_.jpg"

          myimage.Dispose()
          newimage.Dispose()

方法二:

Public Sub MakeSLT(ByVal originalImagePath As String, ByVal thumbnailPath As String, ByVal width As Integer, ByVal height As Integer)
        Dim originalImage As System.Drawing.Image = System.Drawing.Image.FromFile(originalImagePath)
        Dim towidth As Integer = 0
        Dim toheight As Integer = 0
        If originalImage.Width > width AndAlso originalImage.Height < height Then
            towidth = width
            toheight = originalImage.Height
        End If
        If originalImage.Width < width AndAlso originalImage.Height > height Then
            towidth = originalImage.Width
            toheight = height
        End If
        If originalImage.Width > width AndAlso originalImage.Height > height Then
            towidth = width
            toheight = height
        End If
        If originalImage.Width < width AndAlso originalImage.Height < height Then
            towidth = originalImage.Width
            toheight = originalImage.Height
        End If
        Dim x As Integer = 0
        '左上角的x坐标
        Dim y As Integer = 0
        '左上角的y坐标

        '新建一个bmp图片
        Dim bitmap As System.Drawing.Image = New System.Drawing.Bitmap(towidth, toheight)

        '新建一个画板
        Dim g As Graphics = System.Drawing.Graphics.FromImage(bitmap)
        '设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
        '设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
        '清空画布并以透明背景色填充
        g.Clear(Color.Transparent)
        '在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(originalImage, x, y, towidth, toheight)
        Try
            '以jpg格式保存缩略图
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg)
        Catch e As System.Exception
            Throw e
        Finally
            originalImage.Dispose()
            bitmap.Dispose()
            g.Dispose()
        End Try
    End Sub

使用时引用这个函数就可以了。

原创粉丝点击