[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 681
  • Last Modified:

How to save transparent icon? (with code)

procedure Bmp2Ico(bmp, ico: string);
var
  IconSizeX : integer;
  IconSizeY : integer;
  myBmp : TBitmap;
  AndMask : TBitmap;
  XOrMask : TBitmap;
  IconInfo : TIconInfo;
  Icon : TIcon;
begin
  IconSizeX := GetSystemMetrics(SM_CXICON);
  IconSizeY := GetSystemMetrics(SM_CYICON);

  myBmp := TBitmap.Create;
  myBmp.LoadFromFile(bmp);

  AndMask := TBitmap.Create;
  AndMask.Width := IconSizeX;
  AndMask.Height := IconSizeY;
  AndMask.Canvas.Brush.Color := clBlack;
  AndMask.Canvas.Rectangle(0, 0, IconSizeX, IconSizeY);

  XOrMask := TBitmap.Create;
  XOrMask.Width := IconSizeX;
  XOrMask.Height := IconSizeY;
  StretchBlt(XorMask.Canvas.Handle, 0, 0, IconSizeX, IconSizeY,
             myBmp.Canvas.Handle, 0, 0, myBmp.Width, myBmp.Height, SRCCOPY);

  Icon := TIcon.Create;
  IconInfo.fIcon := true;
  IconInfo.xHotspot := 0;
  IconInfo.yHotspot := 0;
  IconInfo.hbmMask := AndMask.Handle;
  IconInfo.hbmColor := XOrMask.Handle;
  Icon.Handle := CreateIconIndirect(IconInfo);

  AndMask.Free;
  XOrMask.Free;
  myBmp.Free;

  Icon.SaveToFile(ico);
  Icon.Free;
end;


if I want the transparent color is white, how to do?
thanks!
0
kiss2
Asked:
kiss2
  • 2
1 Solution
 
nestoruaCommented:
HI,
Yuo could try to do that one of two ways:
1.Before converting your TBitmap to TIcon change the Transparent property of the TBitmap object (it has such property).
2.Change the white pixels of your Bitmap to the color which is
transparent in Icon by default and after that convert Bitmap to Icon.
Sincerely,
Nestorua.
0
 
Lee_NoverCommented:
another workarround would be to make an ImageList and add a bitmap to it and get an icon out :)

function BmpToIco(ABitmap: TBitmap; AIcon: TIcon): Boolean;
var IL: TImageList;
begin
     Result:=false;
     IL:=TImageList.CreateSize(ABitmap.Width, ABitmap.Height);
     try
        IL.AddMasked(ABitmap, ABitmap.TransparentColor);
        IL.GetIcon(0, AIcon);
        Result:=true;
     finally
        FreeAndNil(IL);
     end;
end;

but this doesn't overcome the 16 color problem !
you should also checkout EFG's lab about bitmaps :
http://www.efg2.com/Lab/Library/Delphi/Graphics/BMP.htm
0
 
Lee_NoverCommented:
ok what's wrong with this topic ?
I've already received about 10 mails that a comment has been added by . :)

---------------------------
Question: How to save transparent icon? (with code)
Topic: Delphi Programming

A comment has been added to this question by: .
Click the following URL to read the new comment:
http://www.experts-exchange.com/delphi/Q.20304532.html
---------------------------

moderators whats going on ?
0
 
kiss2Author Commented:
To Lee_Nover:

I don't know.
I've received about 10 mails too.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now