Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Loading Bitmap into ImageList

Posted on 2006-06-30
4
Medium Priority
?
3,461 Views
Last Modified: 2013-12-03
Hi, All
I have saved a ImageList using the ImageEditor to a bitmap file. They are nice small bitmaps on a clFuchsia background. When I try to load them into an ImageList, I can't seem to specifiy the width/height, and the ImageList doesn't seem to figure it out correctly. The bitmap is 9 images by 9 images.
When I load them, the first nine are perfect, but the rest are blank. This is in a component that I am building. Also, no matter how I change the button size, they stay the same!

     fImageList:=TImageList.Create(Self);
     ABitmap:=TBitmap.Create;
     ABitmap.LoadFromFile('c:\PMan\dkcomponents\ToolBitmaps.bmp');
     fImageList.AddMasked(ABitmap, clFuchsia);
     ABitmap.Free;

     for MyCount := 0 to 10 - 1 do begin
        MyButton:=TToolButton.Create(Self);
        with MyButton do begin
           MyButton.Parent:=TWinControl(fToolBar);
           MyButton.Width:=150;
           MyButton.ImageIndex:=MyCount;
           MyButton.Visible:=True;
        end;
     end;

Any Thoughts?
Thanks
0
Comment
Question by:dkeene
[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
4 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 17018947
You could try the following, which manually adds the images (determines image count based on height of loaded image, then dividing width by the height)

Regards,
Russell

var  bmpImages:     TBitmap;
     bmpImage:      TBitmap;
     clTrans:       TColor;
     dwImages:      Integer;
     dwIndex:       Integer;
begin

  FImageList:=TImageList.Create(Self);
  bmpImages:=TBitmap.Create;
  try
     bmpImages.LoadFromFile('c:\PMan\dkcomponents\ToolBitmaps.bmp');
     clTrans:=bmpImages.TransparentColor;
     FImageList.Height:=bmpImages.Height;
     FImageList.Width:=bmpImages.Height;
     bmpImage:=TBitmap.Create;
     try
        bmpImage.Height:=bmpImages.Height;
        bmpImage.Width:=bmpImages.Height;
        if (bmpImages.Height > 0) then
        begin
           dwImages:=bmpImages.Width div bmpImages.Height;
           for dwIndex:=0 to Pred(dwImages) do
           begin
              BitBlt(bmpImage.Canvas.Handle, 0, 0, bmpImage.Width, bmpImage.Height, bmpImages.Canvas.Handle, dwIndex * bmpImage.Width, 0, SRCCOPY);
              FImageList.AddMasked(bmpImage, clTrans);
           end;
        end;
     finally
        bmpImage.Free;
     end;
  finally
     bmpImages.Free;
  end;
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 17029428
rllibby is right, BTW i usually use a faster method to save an imagelist and then load it.

Let's say that you have a simple application with a form, a tbutton and your TImageList called MyImageList with every needed glyph stored in it.

Well, just save you timagelist component as follows:

procedure TForm1.Button1Click(Sender: TObject);
begin
WriteComponentResFile('c:\PMan\dkcomponents\ToolBitmaps.bin',MyImageList);
end;
(You have to do this just one time as you've done for your bitmap)
Well, now that you've saved yor imagelist you can once load it in your component as follows:

FImageList:=TImageList.Create(Self);
try
   ReadComponentResFile('c:\PMan\dkcomponents\ToolBitmaps.bin', FImageList);
except
  MessageDlg('Warning! Images bin file may be corrupted or do not exists!',mtWarning,[mbOK],0);
end;
for MyCount := 0 to FImageList.count - 1 do begin
        MyButton:=TToolButton.Create(Self);
        with MyButton do begin
           MyButton.Parent:=TWinControl(fToolBar);
           MyButton.Width:=150;
           MyButton.ImageIndex:=MyCount;
           MyButton.Visible:=True;
        end;
     end;

That's all. Bin file is smallest than the bitmap, fast loadable and protected (nobody could use it as a bitmap).

Hope this help.

F68 ;-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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…
Suggested Courses

705 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