Solved

How to save transparent icon? (with code)

Posted on 2002-05-24
4
640 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

Industry Leaders: 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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

756 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