0

私はこれをグーグルで検索し、さまざまな記事を読むのにかなりの時間を費やしましたが、ぐるぐる回っているようで、今は非常に混乱しており、どこにもたどり着けていないようです!

これを達成するために、サイズの配列があり、以下の関数をループしています。

これは機能し、期待されるすべてのことを行いますが、小さな画像は非常にブロック状であり、期待する効果を達成するための平滑化を取得できません。

昔ながらの 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このサイズの新しいファイルを作成してオブジェクトに保存しGraphicsます。

これは正しいです?

よりスムーズなサイズ変更を実現するには、何を修正する必要がありますか?

いつものように、どんな助けも大歓迎です。

4

1 に答える 1

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
于 2012-06-28T12:40:59.993 に答える