Bitmap to Icon

I have a TBitmap object and I would like to convert it, programmatically, to a TIcon object.  The Bitmap is 32x32x256 and I would like to convert it to a TIcon so that I can set Application.Icon programmatically.

Does anyone have some sample code as to how to do the conversion without file-io?
JimboKern69Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

edeyCommented:
can you use the TIcon's assign method?  or bitblt to TIcon.handle.

GL
Mike
0
EpsylonCommented:
0
MadshiCommented:
Eps suggestion looks nice. Another possibility would be to go a roundabout way, namely: put the bitmap into an imageList, then ask the imageList to give you an icon...   :-)

Regards, Madshi.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

rwilson032697Commented:
Listening
0
interCommented:
Hi there,

assigning wont work and blitting requires the transparency handling. Instread one may do the following:

procedure TForm1.Button1Click(Sender: TObject);
var
  T : TBitmap;
  Icon : THandle;
  IconInfo : TIconInfo;
begin
  T := TBitmap.Create;
  try
    T.LoadFromFile('icon.bmp');
    T.Transparent := true;
    // create the icon by using mask and bitmap
    with IconInfo do
    begin
      fIcon := true;
      hbmMask := T.MaskHandle;
      hbmColor := T.Handle;
    end;
    Icon := CreateIconIndirect(IconInfo);
    if Icon <> 0 then
    begin
      Application.Icon.Handle := Icon;
      Canvas.Draw(0,0,Application.Icon);
    end else ShowMessage('Could not create icon');
  finally
    T.Free;
  end;
end;

regards,
igor
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EpsylonCommented:
0
JimboKern69Author Commented:
igor...that was a beautifully concise and accurate answer to my question.  The code was easy to implement and worked like a charm.  Add to top it off, that feature of our application has impressed everyone who has seen it thus far.  I've bumped the points up since it has worked so well!

I think the difference between your solution and the Delphi Community solution was that you recognized that the "Bitmap.Transparent = True" line creates an implicit mask that can be used instead of making a seperate one.  Much easier!

Thanks a lot!

Jim
0
interCommented:
you are wellcome and thanks. Note that, the transparent color is choosen by delphi as the pixel at (0,Height-1), so in your bitmap you may paint that pixel to your transparent color. Or you can use TransparentColor property of the bitmap to set the transparent color.
regards,
igor
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.