Ticket T347381
Visible to All Users

Programmatically adding Bitmap images to ImageList using Add method

created 9 years ago

I'm trying to dynamically fill an ImageList with png icons stored in a database. Afterwards, that ImageList will be linked to an ImageComboBox and the icons displayed.

I'm sure the save and load from the database is working just fine, because I checked the temporary file created by the code below. However the problem is on adding images to the ImageList. After that cycle that keeps creating and adding Bitmaps to the ImageList I check the Count field and it has 0 items!!

What am I missing? What doesn't the ImageList.Add() properly work? I tried with and without a bitmap mask and got the same problem.

Delphi
... items.Clear; index := 0; while not Eof do begin // Table (blob) to File tmpIconFilename := FieldByName('IdIcon').AsString + '.png'; tmpIconFilepath := ExtractFilePath(Application.ExeName) + tmpIconFilename; blobIcon := FieldByName('Icon32') as TBlobField; blobIcon.SaveToFile(tmpIconFilepath); // File to Stream streamIcon := TMemoryStream.Create; tmpFileIcon := TPngImage.Create; tmpFileIcon.LoadFromFile(tmpIconFilename); tmpFileIcon.SaveToStream(streamIcon); DeleteFile(tmpIconFilename); // Stream to Bitmap bitmapIcon := TBitmap.Create; bitmapIcon.LoadFromStream(streamIcon); // Bitmap to ImageList maskIcon := TBitmap.Create; maskIcon.Assign(bitmapIcon); maskIcon.Canvas.Brush.Color := clNone; //bkg color maskIcon.Monochrome := true; imagelistNotifPosIcon.Add(bitmapIcon, maskIcon); // Add (icon) item to (icon) combobox Item := Items.Add as TcxImageComboBoxItem; Item.Value := FieldByName('IdIcon').AsString; Item.Description := FieldByName('IdIcon').AsString; Item.ImageIndex := index; index := index + 1; Next; end; ShowInfoMsg('ImageList count: ' + IntToStr(imagelistNotifPosIcon.Count));

Help is much appreciated.

Answers approved by DevExpress Support

created 9 years ago (modified 8 years ago)

Attached is the modified source code that should work in 13.1.3.

    created 9 years ago

    Hello Rui,

    Attached is a small sample project demonstrating how to accomplish this task.

      Comments (1)

        Your code sample doesn't compile with my version of DevExpress (2013 1.3). I can only inspect the code and you're using a non-standard image class (TdxSmartImage) different than mines (TBitmap and TPngImage).

        I try other combinations of code and managed to correctly fill the ImageList using AddBitmap method:

        Delphi
        ... // Stream to Bitmap bitmapIcon := TBitmap.Create; bitmapIcon.LoadFromStream(streamIcon); // Bitmap to ImageList maskIcon := TBitmap.Create; maskIcon.Assign(bitmapIcon); maskIcon.Canvas.Brush.Color := clNone; //bkg color maskIcon.Monochrome := true; imagelistNotifPosIcon.AddBitmap(bitmapIcon, maskIcon, clNone, false, false);

        However, the icons on the combobox are completely black. How do I use the AddBitmap method together with the mask and background color, to correctly display a PNG with transparent background.

        The screenshot shows one PNG manually added versus the ones added by code.

        I had success filling

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.