0

For a given TBitmap instance, I'm using following to try to scale it. It is able to obtain expected size, but alpha information is lost. It is expected to used only on Windows platform.

std::unique_ptr<TBitmap> ScaleBMP(TBitmap* Src, int Width, int Height)
{
  assert(Src && Src->PixelFormat == pf32bit);
  std::unique_ptr<TBitmap> RetVal(new TBitmap());
  RetVal->SetSize(Width, Height);
  RetVal->PixelFormat = pf32bit;
  RetVal->Canvas->StretchDraw(TRect(0, 0, Width, Height), Src);
  return std::move(RetVal);
}

It is preferred not to use additional library to manipulate BMP, although Windows library like WIC or GDI is ok. I couldn't find references about scaling BMP while preserving alpha channel.

smags
  • 33
  • 4
  • you need to do that on your own ... as GDI use the upper 8bit not for ALPHA channel but for some kind interaction mode where valid values are `0,1,2`. So using GDI functions can and usually will destroy your alpha channel. You can manipulate your image easily using `ScanLine[]` property see [#4 VCL/GDI Bitmap](https://stackoverflow.com/a/21699076/2521214) and [Delphi / C++ builder Windows 10 1709 bitmap operations extremely slow](https://stackoverflow.com/a/48901400/2521214) just use nearest neighboar , bilinear or bicubic interpolation ... – Spektre Feb 28 '18 at 09:40
  • In this case, how would I manipulate alpha? Should I use same method against alpha, while calculating RGB? – smags Feb 28 '18 at 16:34
  • yes ... I usually use union of `DWORD` and `BYTE[4]` to easily access the channels. for example see [`union color`](https://stackoverflow.com/a/41779887/2521214) – Spektre Feb 28 '18 at 17:03

0 Answers0