title: Resizing, compressing and smoothing images with VB.net
body: <p>I've spent quite a long time Googling this and read various articles but I seem to have run myself round in circles and am now very confused, and don't seem to have managed to get anywhere!</p>
  
  <p>I am trying to get 4 different sized versions of an image, to achieve this, I have an array of sizes and am looping the function below.</p>
  
  <p>This works, and does everything expected, except, the smaller images are very blocky and I can't get the smoothing to achieve the effect I'd expect.</p>
  
  <p>With old-school ASP Classic, I used Persits ASPUpload/jpeg plugin and it did nice, smooth resizing. With .NET I am struggling.</p>
  
  <pre><code>Public Shared Sub ResizeImages(FileName, NewFileName, maxWidth, maxHeight, uploadDir, qualityPercent)
  
      Dim originalImg As System.Drawing.Image = System.Drawing.Image.FromFile(uploadDir &amp; FileName)
  
      Dim aspectRatio As Double
      Dim newHeight As Integer
      Dim newWidth As Integer
  
      '*** Calculate Size ***'
      If originalImg.Width &gt; maxWidth Or originalImg.Height &gt; maxHeight Then
          If originalImg.Width &gt;= 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 &amp; 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
  </code></pre>
  
  <p>I feel like I might be mixing up Types here?</p>
  
  <p>I have a <code>System.Drawing.Image</code>, a <code>BitMap</code> and a <code>Graphic</code> object during the course of the function above, and feel that I might be trying to apply to the smoothing to the wrong element, or at the wrong stage of the coding?</p>
  
  <p>The <code>System.Drawing.Image</code> object loads the file that's saved on the server, then I calculate the aspect ratio and new size required, create a new <code>BitMap</code> of this size and save it to a <code>Graphics</code> object.</p>
  
  <p>Is this correct?</p>
  
  <p>What do I need to amend to achieve a smoother resize?</p>
  
  <p>As usual, any help much appreciated.</p>