Why am I losing transparency when calling BitBlt or CopyRect?

心不动则不痛 提交于 2019-12-05 10:39:48

There are 3 popular ways to work with transparent bitmaps, first two use standard Delphi tools and the third one requires a third-party library:

If you use one of the two standard methods, don’t use BitBlt or CopyRect. Use the Draw method of the transparent image holder to draw on destination canvas.

  1. Keep your transparent bitmaps in a TImageList and use TImageList.Draw to paint directly on destination canvas (don’t paint on intermediary bitmaps since you will lose transparency here). To add images to an image list at design time, right click and choose Image List Editor. Images in the list may be bitmaps, icons, PNG, GIF and JPEG images: any image type that TImage supports. ImageLists also support 32-bit format, so alpha blended bitmaps and PNG files work properly. You can also load images at run time. If your bitmaps are stored in a non-transparent form but there is a transparent color, you can use the TImageList.AddMasked(Bitmap: TBitmap; MaskColor: TColor) method. You can either pass the transparent color yourself in the second parameter or clDefault to let the imagelist take the bottom-left pixel's color.
  2. Keep your images in PNG files or resources and load them into a Vcl.Imaging.pngimage.TpngImage, then call TpngImage.Draw to paint your PNG directly on destination canvas. As above, don’t paint on intermediary bitmaps since you will lose transparency here.
  3. Use TBitmap32 from GR32, a third-party library. In this case, Do not use TBitmap32 with transparent images to draw directly on HDC, Canvas or TBitmap. Use dmBlend and DrawTo or BlockTransfer() to draw on another TBitmap32. For example, to draw transparently on a TBitmap, create an intermediary cache TBitmap32: (1) Copy the image from TBitmap to the cache TBitmap32; (2) Apply your transparent image to the cache TBitmap32 using DrawTo or BlockTransfer(), avoid using Canvas or HDC to mix two images because they lose alpha layer information; (3) Copy the image back from the cache TBitmap32 to your TBitmap.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!