I'm using Delphi 2009 and I'd like to scale an image to fit the available space. the image is always displayed smaller than the original. the problem is TImage Stretch property doesn't do a nice job and harms the picture's readability.
ugly way http://xrw.bc.ca/download/so/TImageStretch.gif
I'd like to see it scaled like this instead:
nicer way http://xrw.bc.ca/download/so/NicerTImageStretch.png
Any suggestions how best to do this? Tried JVCL, but it doesn't seem to have this ability. A free library would be nice but maybe there's a low cost library that does "only" this would be good as well.
You could try the built-in Delphi ScaleImage from GraphUtil
I use GDIPOB.pas's TGPGraphics class
if Canvas is TGPGraphics, Bounds is TGPRectF and NewImage is TGPImage instance:
You can choose the quality VS speed factor by changing the interpolation mode
and smooting mode:
NOTE: This would require XP or later or bundeling the gdiplus.dll in your installer.
If you revert to using Win32 API calls, you can use SetStretchBltMode to HALFTONE and use StretchBlt. I'm not sure if this is provided using default Delphi calls, but that's the way I generally solve this issue.
Update (2014-09) Just now I was in a similar situation (again) and had a TImage in a TScrollBox with lots more going on on the form, and really wanted
Image1.Stretch:=true;
to do halftone. As Rob points out,TBitmap.Draw
uses HALFTONE only when the destination canvas is 8 bits-per-pixel or lower and the source canvas has more... So I 'fixed' it with assigningImage1.Picture.Bitmap
to one of these instead:You really, really want to use Graphics32.
You have several methods and filters to choose when resampling an image. The example above uses a kernel resampler (kinda slow, but with great results) and a Lanczos filter as reconstruction kernel. The above example should work for you.