1

I am using code such as the following to crop images:

Dim oImg As System.Drawing.Image = System.Drawing.Image.FromFile(sSrc)

'prep new image
Dim oCanvas As System.Drawing.Bitmap
Select Case oImg.PixelFormat
    Case 198659, 197634, 196865, 65536 'indexed formats (as found in gifs) cant be used to make a graphic object
        oCanvas = New System.Drawing.Bitmap(w, h, PixelFormat.Format32bppArgb)
    Case Else 'use src img's pixel format
        oCanvas = New System.Drawing.Bitmap(w, h, oImg.PixelFormat)             
End Select
oCanvas.MakeTransparent()


Dim oGraphic As Graphics = Graphics.FromImage(oCanvas)
oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
oGraphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
oGraphic.Clear(Color.Transparent)
oGraphic.DrawImage(oImg, New Rectangle(0, 0, w, h), X, Y, w, h, GraphicsUnit.Pixel)


Select Case sTgtFileExt
    Case "jpg"
        Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
        Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
        Dim myEncoderParams As New EncoderParameters(1)
        Dim myEncoderQuality As New EncoderParameter(myEncoder, CType(98L, Int32)) '98%
        myEncoderParams.Param(0) = myEncoderQuality
        oCanvas.Save(sTgt, jpgEncoder, myEncoderParams)
    Case "png", "gif"
        oCanvas.Save(sTgt, System.Drawing.Imaging.ImageFormat.Png)
    Case "tiff", "tif"
        oCanvas.Save(sTgt, System.Drawing.Imaging.ImageFormat.Tiff)
    Case Else
        oCanvas.Save(sTgt, System.Drawing.Imaging.ImageFormat.Png)
End Select

Specifically for cropping .jpg source files, I am ending up with cropped images which are 5-8 times filesize than the un-cropped original.

For example, I can input an image which is, say, 800kb in size, and crop middle half of it into a new image, which, when saved is over 5mb.

If I don't leave the jpeg quality at 95% or higher, I get an image that is lower in quality than the original but still the same or more in filesize.

What I want is an image that is the same quality as the original. What is it about the code that causes the huge increase in size, even for just a cropped section of the original?

GWR
  • 1,878
  • 4
  • 26
  • 44

1 Answers1

0

Found the bug... it is unrelated to image processing. Was a bug in my final case statement which chooses the encoder. My code which captures the extension had the "." character present, but my case names didn't, thus, it was automatically hitting the else case, which defaults the image type to png, which is of course a lossless format, and has a higher file size in general

GWR
  • 1,878
  • 4
  • 26
  • 44
  • Re-saving as 98% jpeg is also bound to increase size, though. You might want to look into lossless jpeg cropping tools, though they are always limited by the jpeg block size. – Nyerguds Nov 23 '17 at 12:50