2

I want to blit 32bit image (PNG with alpha channel) over 32bit Bitmap (or Bitmap32).

I don't know how to tell function to blend image instead of bliting it. Like using AlphaBlend instead of BitBlt, I need to make this function to blend image instead of simple blit.

This is my example:

ABitmap is type of TBitmap32 (GR32), but the same result is with regular TBitmap with 32bit palette.

    ABitmap.SetSize(GetWidth, GetHeight);
    SetStretchBltMode(ABitmap.Handle, COLORONCOLOR);//HALFTONE);
    ABitmap.Clear($FFFFFFFF);  { Here I try to erase background with white color, so that new image is blit over white surface }
    StretchDIBits(
      ABitmap.Handle,
      0, 0,
      GetWidth, GetHeight,
      0, 0,
      GetWidth, GetHeight,
      FreeImage_GetBits(Dib),
      FreeImage_GetInfo(Dib)^,
      DIB_RGB_COLORS,
      SRCCOPY);    // --- SRCAND etc. produce not good results

FreeImage_GetBits and FreeImage_GetInfo are FreeImage library functions that point to the start of bit array and point to the structure.

What ever I do StretchDBit doesn't allow blending.

Ivan Mark
  • 463
  • 1
  • 5
  • 17

1 Answers1

2

StretchDIBits() does not have any concept of alpha-blending. You will have to blit your PNG to a temporary 32-bit bitmap first, making sure to set up its alpha channel as needed, and then AlphaBlend() the temporary bitmap onto your destination bitmap. Or, you could just AlphaBlend() the PNG directly to the destination bitmap if the PNG is laid out in memory using a 32-bit bitmap already.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The whole concept of alpha blending is foreign to most GDI functions. It was bolted on very late in the game. – Mark Ransom May 11 '12 at 02:27
  • 2
    True. Even later in the game, the VCL's `TCanvas.Draw()` method just in the last year or two finally received support for alpha-blending via `AlphaBlend()` when drawing bitmaps. – Remy Lebeau May 11 '12 at 02:32
  • AlphaBlend function expect Canvas.Handle (HDC) instead of memory array. It's DDB and not DIB (Device Independed Bitmap) function. I'm looking some DIB blend function. – Ivan Mark May 11 '12 at 22:07
  • `AlphaBlend()` works with DIBs. [Microsoft's own example](http://msdn.microsoft.com/en-us/library/dd183353.aspx) shows a DIB being used. – Remy Lebeau May 11 '12 at 22:58