私はこれをグーグルで検索し、さまざまな記事を読むのにかなりの時間を費やしましたが、ぐるぐる回っているようで、今は非常に混乱しており、どこにもたどり着けていないようです!
これを達成するために、サイズの配列があり、以下の関数をループしています。
これは機能し、期待されるすべてのことを行いますが、小さな画像は非常にブロック状であり、期待する効果を達成するための平滑化を取得できません。
昔ながらの 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
ます。
これは正しいです?
よりスムーズなサイズ変更を実現するには、何を修正する必要がありますか?
いつものように、どんな助けも大歓迎です。