First, just to say, I know there are a ton of questions and answers here about image resizing in .NET, and believe me I have spent hours looking into this. I find it intriguing, and still feel I must be missing something, that somehow a simple PDF file can display an image at any size it wants and retain a far higher image quality than can be obtained using .NET.
I have an PNG image at 1000x1000px. It has some embedded text.
Using the iTextSharp PDF libray, I save it in a PDF file at 300×300:
.ScaleAbsolute(300, 300)
The result is very good – a nice high quality image, showing clear text.
Then, to try and reduce the file size, I use .NET to first reduce the image size – pretty standard .NET stuff – using a graphics object g:
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.CompositingQuality = CompositingQuality.HighQuality
g.SmoothingMode = SmoothingMode.HighQuality
g.PixelOffsetMode = PixelOffsetMode.HighQuality
Save this, and again create my PDF.
The resulting image, viewed in the PDF or even directly in an image viewer, is of a far lower quality, particularly noticeable around the text.
My question is: if a PDF can resize an image on the fly and retain its high quality, why can’t .NET create one to begin with? Or can it? Is there some method other than the standard .NET code that can achieve this?
After some Googling, I did try an alternative method, using a helper function
Friend Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim j As Integer
Dim encoders As ImageCodecInfo()
encoders = ImageCodecInfo.GetImageEncoders()
For j = 0 To encoders.Length
If encoders(j).MimeType = mimeType Then
'adp.WriteLog("", "got it " & mimeType, True)
Return encoders(j)
End If
Next j
Return Nothing
End Function
and then in the main routine:
Dim compression As Long = 255 ' lower value is poor quality, high is better.
Dim myImageCodecInfo As ImageCodecInfo
Dim myEncoder As Encoder
Dim myEncoderParameter As EncoderParameter
Dim myEncoderParameters As EncoderParameters
myImageCodecInfo = GetEncoderInfo("image/png")
myEncoder = Encoder.Quality
myEncoderParameters = New EncoderParameters(1)
myEncoderParameter = New EncoderParameter(myEncoder, compression)
myEncoderParameters.Param(0) = myEncoderParameter
then the standard graphics code, and then saving it with
Save([path], myImageCodecInfo, myEncoderParameters)
but tbh I couldn’t see any difference in the resulting image to the original straightforward method. (I’m not really sure it’s nothing more than a fancy way of simply asking for it be saved in PNG Format anyway, but IDK.)
I have different InterpolationMode settings.
Anyway, back to my question: is there a better way of saving a resized image in HQ? PDF’s manage it easily enough, so can’t .NET? What’s a PDF doing that .NET isn’t? In theory, I’d get a better result from taking a screenshot of the original image resized in the PDF!