?
Solved

How to save transparent icon? (with code)

Posted on 2002-05-24
4
Medium Priority
?
658 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 300 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

777 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