Solved

How to save transparent icon? (with code)

Posted on 2002-05-24
4
609 Views
Last Modified: 2012-05-04
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
Comment
Question by:kiss2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:nestorua
Comment Utility
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
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 100 total points
Comment Utility
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
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
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
 

Author Comment

by:kiss2
Comment Utility
To Lee_Nover:

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now