c++mfcstretchbltcimage

MFC CImage weird artifacts


I am using MFCs CDC to try to create output and am currently trying to render some images.

In my example I want the output to be around 300x300 pixels, but the original picture is 2360x1884. When loading this image into a CImage and drawing it using a CRect being 300x300, the result is really crazy...

Here is the original image:

Original image

Drawn image:

Printed Image

How do I properly draw an image where the original image is larger?

Sample code:

  CImage image;
  CDC* painter;
  CString filepath = "puppy.jpg";
  image.Load(filepath);
  CRect boundries(x, y, x + 300, y + 300);
  HDC hdc = *painter;
  image.Draw(hdc, boundries);

Solution

  • By default the Draw method will use the stretching Blt mode BLACKONWHITE which is fine if the image is the same size, however it will cause the color artifacting that you see when shrinking an image, as can be seen from the image below:

    enter image description here

    By setting the Stretch Blt Mode to HALFTONE, the correct color behaviour can then be observered, all that is required is to add a call to SetStretchBltMode before you use Draw:

    SetStretchBltMode(hdc, HALFTONE);
    image.Draw(hdc, boundries);
    

    enter image description here