• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

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?
0
JimboKern69
Asked:
JimboKern69
1 Solution
 
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
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
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
 
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now