少し前に、自分のWebサイトのバックエンド用の画像アップロード機能を作成しようとしていました。なんとかこれを達成できましたが、小さい画像ではまだ画質が悪くなっています。
4つの画像を作成する必要があります。
- 画像をズーム1800x1800 px max
- 画像を表示する最大180x275px
- 画像検索120x100px max
- 小さなサムネイル50x50px max
私は通常、アップロードする前に、Photoshopなどを使用して手動でサイズ変更して1800 x 1800に画像化し、次に以下のコードを使用してアップロードおよびサイズ変更します(画像はすべてjpgです)
変数は次のとおりです。
- FileName=最初に正常にアップロードされました
- NewFileName=サイズ変更された画像を名前を付けて保存するファイル名
- maxWidth/maxHeight-自明
- uploadDir=保存先のディレクトリ
解像度=品質jpg解像度0〜100、これらの例では80を使用しています
Public Shared Sub ResizeImages(FileName, NewFileName, maxWidth, maxHeight, uploadDir, resolution) Try 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(resolution, Int32)) myEncoderParameters.Param(0) = myEncoderParameter canvas.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality canvas.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality canvas.DrawImage(newImg, New Rectangle(0, 0, newWidth, newHeight)) newImg.Save(uploadDir & (NewFileName), getCodec("image/jpeg"), myEncoderParameters) '*** Close ***' canvas.Dispose() originalImg.Dispose() newImg.Dispose() '*** Nothing ***' canvas = Nothing newImg = Nothing originalImg = Nothing Catch ex As Exception HttpContext.Current.Response.Write(ex.ToString & " " & uploadDir & " " & FileName & " _ " & NewFileName) End Try End Sub
4つの画像すべてを実現するために、必要なサイズをリストとして渡し、そのリストを目的のファイルサイズの降順でループします。つまり、最初に大きいものから、最後にアップロードした画像をFileName
パラメーターとして関数に渡します。そのため、関数は毎回小さい画像を受信するため、さまざまな投稿を読んでわかったように、2000x2000pxの画像のサイズを50x50pxに変更しようとしないでください。これを大幅に減らすと、品質が低下します。
この方法でループを実行した後、私の小さなサムネイルは非常に高品質ですが、私の中間の画像はまだ貧弱です。
ここでは、サイズの降順です。
http://www.hartnollguitars.co.uk/Products2/0/0/0/9/6/57/1347831580.jpg http://www.hartnollguitars.co.uk/Products2/0/0/0/9 / 6/57/ 1347831580-dis.jpg http://www.hartnollguitars.co.uk/Products2/0/0/0/9/6/57/1347831580-se.jpg http://www.hartnollguitars.co .uk / Products2 / 0/0/0/9/6/57 / 1347831580-tb.jpg
ご覧のとおり、「検索」と「表示」の両方の画像は、ギターの端の周りでまだブロックされています。
私は何が間違っているのですか?!
私の削減が多すぎる場合、メモリ内の段階的な削減を実行するにはどうすればよいでしょうか。
これが意味するのは、上記の機能は毎回ファイルをディスクに保存することであり、これには時間がかかる必要があるため、縮小機能をループして画像のサイズを小さくする場合は、メモリ内で増分(たとえば一度に10%)し、縮小が正しいサイズに達したときに最終的な画像をディスクに保存します。しかし、これを行う方法がわかりません。
私はASP.NET2.0を使用していて、比較的新しいので、使用できるすべての方法を完全には認識していません。
どんなコード例も大いに役立ちます!
ありがとう