Recent

Author Topic: TImageList issue - preserving transparency - me/mac/cocoa issue?  (Read 1202 times)

MISV

  • Hero Member
  • *****
  • Posts: 783
Not quite sure where to post... So I also posted this in programming/general forum: https://forum.lazarus.freepascal.org/index.php/topic,47033.0.html

I am basically trying to resize images in my TImageList.

Since that caused problems I simplified my problem to better narrow it down. Hence in this example all I am actually doing is moving images from one imagelist to another and then moving them back.... But I am losing transparency in the process. (One can see this visually by by e.g. have the imagelist connected to a toolbar or similar.)

Code: Pascal  [Select][+][-]
  1. procedure myMoveImages(const AImageList: TImageList);
  2. var
  3.   TmpIter: Integer;
  4.   TmpBitmapOldmask: TBitmap;
  5.   TmpBitmapOldImg: TBitmap;
  6.   TmpImageListCopyHolder: TImageList;
  7. begin
  8.   TmpImageListCopyHolder := TImageList.Create(nil);
  9.   TmpImageListCopyHolder.Width := AImageList.Width;
  10.   TmpImageListCopyHolder.Height := AImageList.Height;
  11.   TmpImageListCopyHolder.DrawingStyle := AImageList.DrawingStyle;
  12.   //--
  13.   TmpBitmapOldImg := myCreateBitmap(AImageList.Width, AImageList.Height, clNone, pf32bit);
  14.   TmpBitmapOldmask := myCreateBitmap(AImageList.Width, AImageList.Height, clNone, pf32bit);
  15.   //--
  16.   for TmpIter := 0 to -1 + AImageList.Count do
  17.   begin
  18.     TmpBitmapOldImg.Canvas.FillRect(TmpBitmapOldImg.Canvas.ClipRect);
  19.     TmpBitmapOldmask.Canvas.FillRect(TmpBitmapOldmask.Canvas.ClipRect);
  20.     AImageList.Draw(TmpBitmapOldImg.Canvas, 0, 0, TmpIter, dsTransparent, itImage, True);
  21.     AImageList.Draw(TmpBitmapOldMask.Canvas, 0, 0, TmpIter, dsNormal, itMask, True);
  22.     //TmpImageListCopyHolder.Add(TmpBitmapOldImg, TmpBitmapOldMask);
  23.     TmpImageListCopyHolder.AddMasked(TmpBitmapOldImg, TmpBitmapOldImg.TransparentColor);
  24.   end;
  25.   //--
  26.   AImageList.Clear;
  27.   for TmpIter := 0 to -1 + TmpImageListCopyHolder.Count do
  28.   begin
  29.     TmpBitmapOldImg.Canvas.FillRect(TmpBitmapOldImg.Canvas.ClipRect);
  30.     TmpBitmapOldmask.Canvas.FillRect(TmpBitmapOldmask.Canvas.ClipRect);
  31.     TmpImageListCopyHolder.Draw(TmpBitmapOldImg.Canvas, 0, 0, TmpIter, dsTransparent, itImage, True);
  32.     TmpImageListCopyHolder.Draw(TmpBitmapOldMask.Canvas, 0, 0, TmpIter, dsNormal, itMask, True);
  33.     AImageList.AddMasked(TmpBitmapOldImg, TmpBitmapOldImg.TransparentColor);
  34.     //AImageList.Add(TmpBitmapOldImg, TmpBitmapOldMask);
  35.   end;
  36.  
  37.  
  38. function myCreateBitmap(AWidth, AHeight: Integer; ABGColor: TColor; APixelFormat: TPixelFormat): TBitMap;
  39. var
  40.   BitMap: TBitMap;
  41.   TmpRect: TRect;
  42. begin
  43.    BitMap := TBitMap.Create;
  44.    with BitMap do
  45.      begin
  46.        PixelFormat := APixelFormat;
  47.        Width := AWidth;
  48.        Height := AHeight;
  49.        Canvas.Brush.Style := bsSolid;
  50.        if ABGColor = clNone then
  51.          ABGColor := Bitmap.TransparentColor
  52.        ;
  53.        Canvas.Brush.Color := ABGColor;
  54.        TmpRect.Top := 0;
  55.        TmpRect.Left := 0;
  56.        TmpRect.Right := Width;
  57.        TmpRect.Bottom := Height;
  58.        Canvas.FillRect(TmpRect);
  59.      end
  60.    ;
  61.    Result := BitMap;
  62. end;
  63.  

...

Instead of above I have also tried

1)
Code: Pascal  [Select][+][-]
  1.     myImageList1.Draw(TmpBitmapOldImg.Canvas, 0, 0, TmpIter, dsNormal, itImage, True);
  2.     myImageList1.Draw(TmpBitmapOldMask.Canvas, 0, 0, TmpIter, dsNormal, itMask, True);
  3.     myImageList2.Add(TmpBitmapOldImg, TmpBitmapOldMask);
  4.  


3)
Tried everything could thing of with drawingstyle bkcolor etc.

...

Does anyone know why I loose transparency? I have tried everything I can think of. I also have the same code working on Windows/Delphi.
« Last Edit: October 11, 2019, 04:22:17 pm by MISV »

MISV

  • Hero Member
  • *****
  • Posts: 783
Re: TImageList issue - preserving transparency - me/mac/cocoa issue?
« Reply #1 on: October 12, 2019, 01:41:21 pm »
It is a bug in LCL Cocoa ... I am now 99% sure :)

Someone posted ar demo project working for them on Windows - I just tried it on LCL Cocoa on Mac - same problem as what I am experiencing... And I don't think there is an easy solution because I have tried 50+ different attempts at solving using slightly different methods (including the same method used in the demo project posted/attached by another in the other programming-general thread linked in my first post in this thread)

MISV

  • Hero Member
  • *****
  • Posts: 783
Re: TImageList issue - preserving transparency - me/mac/cocoa issue?
« Reply #2 on: October 12, 2019, 03:33:38 pm »
here is demo project

 

TinyPortal © 2005-2018