Recent

Author Topic: Icon Transparent color problum  (Read 5545 times)

BenJones

  • Full Member
  • ***
  • Posts: 121
Icon Transparent color problum
« on: May 09, 2013, 05:58:12 am »
hi I am trying to convert a bitmap to a icon, with the image list, it does save tho the transparent color does not, I tested in delphi 7 and it works fine any ideas what's wrong here my code.

procedure Tfrmmain.cmdConvertClick(Sender: TObject);
var
  ico : TIcon;
begin
   ico := CreateIconFromBitmap(Image1.Picture.Bitmap,clFuchsia);
   ico.SaveToFile('c:\out\new.ico');
end;

function CreateIconFromBitmap(Bitmap: TBitmap; TransparentColor: TColor): TIcon;
begin
  with TImageList.CreateSize(Bitmap.Width, Bitmap.Height) do
  begin
    try
      AllocBy := 1;
      AddMasked(Bitmap, TransparentColor);
      Result := TIcon.Create;
      try
        GetIcon(0, Result);
      except
        Result.Free;
        raise;
      end;
    finally
      Free;
    end;
  end;
end;

The bitmap is also just a 32x32x16 color
When Your Dreams Come True.

pyros

  • New Member
  • *
  • Posts: 17
Re: Icon Transparent color problum
« Reply #1 on: May 16, 2013, 05:34:51 pm »
Hello Ben Jones,

you are in luck, since I happened to have spent all afternoon attempting to find the same solution.

Anyway, I can't summon any further energy to wade through your code, but this will use a bitmap to make an icon and then save it with transparency (red in this case).

Code: [Select]
IconPicture := TPicture.Create;
IconPicture.Bitmap.SetSize(32, 32);
IconPicture.Bitmap.Canvas.StretchDraw(Types.Rect(0, 0, 32, 32), SourceBitmap);
IconPicture.Bitmap.Mask(clRed);
IconPicture.Icon.SaveToFile(Folder + 'favicon.ico');
IconPicture.Free;

Apparently there is also a class TKIcon that supports full alpha!:

http://www.lazarus.freepascal.org/index.php?topic=10629.0

 

TinyPortal © 2005-2018