Solved

How to save transparent icon? (with code)

Posted on 2002-05-24
4
651 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:nestorua
ID: 7034228
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
ID: 7034412
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
ID: 7035579
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
ID: 7036440
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

690 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