The following code,adopted from here, flips and paints a TBitmap on a TPaintbox.
void flipImageVertically(Graphics::TBitmap *pBitmap, TPaintBox* paintBox)
{
int w = pBitmap->Width;
int h = pBitmap->Height;
TRect dest(getStretchedDimensions(w, h, paintBox->Width, paintBox->Height));
TRect src(Rect(0, h, w, 0));
Graphics::TBitmap *bmp;
bmp = new TBitmap;
bmp->PixelFormat = pBitmap->PixelFormat;
bmp->SetSize(w,h);
bmp->Canvas->Draw(0, 0, pBitmap);
paintBox->Canvas->CopyRect(dest, bmp->Canvas, src);
delete bmp;
}
However, compared to the code that just paints and stretches an image to the paintbox:
TRect stretchedRect(getStretchedDimensions(tbm->Width, tbm->Height, paintBox->Width, paintBox->Height));
paintBox->Canvas->StretchDraw(stretchedRect, tbm);
the flipped image have 'gleaming', looking over saturated pixels. I believe it has todo with the
bmp->Canvas->Draw(0, 0, pBitmap);
paintBox->Canvas->CopyRect(dest, bmp->Canvas, src);
lines of code, that don't involve a 'stretched' draw.
Trying changing the above code to using a stretched draw:
paintBox->Canvas->StretchDraw(dest, bmp);
instead of CopyRect don't work. It still shows gleaming pixels and it is not flipped. (Just realized the above code flips the image horizontally). The bitmap is a 8bit greyscale bitmap.
Update Changing the pixel format to pf32Bit does fix most of the problems. However, when stretched to a smaller than original dimension, black 'squares' and other patterns emerge on various part of the image. See below: