使用VB.net调整图像大小,压缩和平滑

时间:2012-06-28 12:34:15

标签: vb.net image-processing graphics bitmap system.drawing

我花了很长时间用Google搜索并阅读各种文章,但我似乎已经绕圈子跑了,现在我很困惑,似乎没有设法到达任何地方!

我正在尝试获得4个不同大小的图像版本,为了达到这个目的,我有一个大小的数组,并且循环使用下面的函数。

这很有效,并且做了所有预期的事情,除了较小的图像是非常块状的,我无法通过平滑来达到我期望的效果。

使用旧式ASP Classic,我使用Persits ASPUpload / jpeg插件,它做得很好,顺利调整大小。使用.NET,我很挣扎。

Public Shared Sub ResizeImages(FileName, NewFileName, maxWidth, maxHeight, uploadDir, qualityPercent)

    Dim originalImg As System.Drawing.Image = System.Drawing.Image.FromFile(uploadDir & FileName)

    Dim aspectRatio As Double
    Dim newHeight As Integer
    Dim newWidth As Integer

    '*** Calculate Size ***'
    If originalImg.Width > maxWidth Or originalImg.Height > maxHeight Then
        If originalImg.Width >= originalImg.Height Then ' image is wider than tall
            newWidth = maxWidth
            aspectRatio = originalImg.Width / maxWidth
            newHeight = originalImg.Height / aspectRatio
        Else ' image is taller than wide
            newHeight = maxHeight
            aspectRatio = originalImg.Height / maxHeight
            newWidth = originalImg.Width / aspectRatio
        End If
    Else ' if image is not larger than max then keep original size
        newWidth = originalImg.Width
        newHeight = originalImg.Height
    End If

    Dim newImg As New Bitmap(originalImg, CInt(newWidth), CInt(newHeight)) '' blank canvas
    Dim canvas As Graphics = Graphics.FromImage(newImg) 'graphics element

    '*** compress ***'
    Dim myEncoderParameters As EncoderParameters
    myEncoderParameters = New EncoderParameters(1)
    ' set quality level based on "resolution" variable
    Dim myEncoderParameter = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, CType(qualityPercent, Int32))
    myEncoderParameters.Param(0) = myEncoderParameter

    '*** Save As  ***'
    canvas.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
    canvas.DrawImage(newImg, New Point(0, 0))
    newImg.Save(uploadDir & NewFileName, getCodec("image/jpeg"), myEncoderParameters)

    '*** Close ***'
    originalImg.Dispose()
    newImg.Dispose()
    '*** Nothing ***'
    newImg = Nothing
    originalImg = Nothing

End Sub

Public Shared Function getCodec(getThis) As Drawing.Imaging.ImageCodecInfo
    Dim output As Drawing.Imaging.ImageCodecInfo
    Dim codecs As Imaging.ImageCodecInfo() = Imaging.ImageCodecInfo.GetImageEncoders
    For Each codec As Imaging.ImageCodecInfo In codecs
        If codec.MimeType = getThis Then
            output = codec
        End If
    Next codec
    Return output
End Function

我觉得我可能会在这里混淆类型?

我在上面的函数过程中有一个System.Drawing.Image,一个BitMap和一个Graphic对象,觉得我可能会尝试将平滑应用到错误的元素,或编码的错误阶段?

System.Drawing.Image对象加载保存在服务器上的文件,然后我计算所需的宽高比和新大小,创建一个这个大小的新BitMap并将其保存到{{1}对象。

这是对的吗?

我需要修改什么来实现更平滑的调整大小?

像往常一样,任何帮助都非常感激。

1 个答案:

答案 0 :(得分:2)

尝试使用 InterpolationMode

Private imgSource As System.Drawing.Image
Private imgOutput As System.Drawing.Image
Public Function Resize(ByVal intPercent As Integer, ByVal intType As Integer)
    'resize the image by percent
    Dim intX, intY As Integer
    intX = Int(imgSource.Width / 100 * intPercent)
    intY = Int(imgSource.Height / 100 * intPercent)
    Dim bm As Drawing.Bitmap = New System.Drawing.Bitmap(intX, intY)
    Dim g As System.Drawing.Graphics = Drawing.Graphics.FromImage(bm)

    Select Case intType
        Case 0
            g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.Default
        Case 1
            g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High
        Case 2
            g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBilinear
        Case 3
            g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
    End Select

    g.DrawImage(imgSource, 0, 0, intX, intY)
    imgOutput = bm

End Function