How to assign a graphic to an FMX TImage in the IDE? - delphi

In the Delphi IDE, how do you assign a graphic to a TImage?
In VCL, in the Object Inspector, you use the "Picture" property of the TImage. But in FMX, I don't see "Picture" in the Object Inspector, or anything else like it ("Bitmap", "Graphic", etc.)
Please have mercy. I have 20+ years experience in Delphi VCL, but I'm a raw newbie in FireMonkey!

The property you're looking for is MultiResBitmap. It's use is covered in the documentation under Using Multi-Resolution Bitmaps. The portion pertaining to TImage:
In TImage controls. TImage controls keep a TFixedMultiResBitmap multi-resolution bitmap in the MultiResBitmap property. TFixedMultiResBitmap is the descendant of TCustomMultiResBitmap. A TFixedMultiResBitmap multi-resolution bitmap can contain any number of bitmap items having different scales. On each device, TImage retrieves the most appropriate bitmap to display from the bitmap collection in the TFixedMultiResBitmap multi-resolution bitmap and refers to the obtained bitmap with the Bitmap property. The obtained bitmap depends on the device resolution and scales of bitmap items kept in the TFixedMultiResBitmap multi-resolution bitmap. If a multi-resolution bitmap does not contain a bitmap item having exactly the scale required by some particular screen, then FireMonkey automatically stretches or zooms out the bitmap item having the most appropriate scale. For information about how this bitmap is obtained, see Bitmap. Keep in mind that each bitmap item takes resources of the application's executable on all platforms (even if some bitmap item is never used on a particular platform).

Related

brcc32 invalid bitmap format

i created a delphi component and i want to add an icon to it, i know the procedure to follow, but something does not work for me, so here is what I did:
I Created a bitmap file.
I Created an rc file (MyComponent.rc) using notepad and added this to it: TMyComponent BITMAP "MyComponent.bmp" , as my component name is:TMyComponent.
I tried to get the res file using delphi ressource compiler : brcc32 Mycomponent.rc, but I'm getting error 1 33:invalid bitmap format.
I tried to use the other alternative which is image editor, but there is no option to make a res file, may be I'm using the inappropriate software.
Why do I get this error? or could you just give me a link to get the right image editor? Thanks for your help.
Make sure your .bmp file is 8-bit (256 colors) and is 24x24 pixels in size.
Also, the resource name needs to be the component class type in all caps:
TMYCOMPONENT BITMAP "MyComponent.bmp"
Also, the IDE supports 16x16, 24x24, and 32x32 component icons, so you should include 16x16 and 32x32 bitmaps in your resource as well (otherwise the IDE will resize the 24x24 bitmap when needed, which might not look good when shrunk/stretched):
TMYCOMPONENT BITMAP "MyComponent24x24.bmp"
TMYCOMPONENT16 BITMAP "MyComponent16x16.bmp"
TMYCOMPONENT32 BITMAP "MyComponent32x32.bmp"`

Delphi: TPicture from TImageList

How to get a TPicture from a TImageList?
I need to do Image1.Picture:=...TPicture from an image list, to load an image into a TImage.
An image list stores all my PNG images that are transparent.
I tried to use a bitmap (GetBitmap), but what I need is transparency. Unfortunatelly, I have a white background using a bitmap.
Thanks!
The regular TImageList uses bitmaps. Although they can be partially transparent, it is actually just a fake. In Delphi you can draw bitmaps to be transparent by assigning them a single transparent color. That exact color will be drawn a 100% transparent, while the other colors are not. Usually the color is taken from the bottom left corner of the image.
TPicture itself doesn't do anything. It is merely a container for TGraphic descendants. You'll have to find a type of image that can be transparent.
A convenient format is PNG. PNG event supports an alpha channel, which means that every pixel can be assigned a different transparency value.
Fortunately there are TPngImageLists that combine the ease of TImageList with the power of PNG. You can read this article. It is in Dutch, but maybe Google Translate can help you. Or maybe you can find an english resource on this subject. I've used this imagelist and it's great, because you can have actual icons with an alpha channel and still use them with regular toolbars and speedbuttons.

Clipping a filmstrip in png format (Delphi 2010)

I have a filmstrip of images in png format like this:
I'd like to know how to clip each of the images and put these images in a TImageList control, always preserving the transparency.
[EDIT]
Yes, at designtime the trick mentioned by RRUZ works fine, but I wanted to clip the images at runtime, i.e. by loading the filmstrip from resource or file
You must follow these steps:
set ColorDepth property to cd32Bit,
DrawingStyle to dsTransparent,
Height= 48,
Width=48,
then load the image and the result will be
Just import into the imagelist. It'll complain that it's too big and offer to break it into pieces for you. Works fine for me on D2005.
Another cool tip: I use AWIcons Pro http://www.awicons.com/icon-editor/ to edit icons (nice editor!). It has a feature that can export an icon as an imagelist (.bmp or .png format), thus making the filmstrip out of an icon. This makes it really handy to edit these things in .ico format, with a series of cells all the same size and depth, with each cell varying slightly. Then you export as an imagelist (I use .png) and then Delphi can break them back out into individual cells. Very slick. AWIcons isn't free, but features like this really make it productive.
At runtime, you would have to call TImageList.FileLoad. Except it won't work.
This in turn calls ImageList_LoadImage, with uFlags parameter value including the bit LR_LOADFROMFILE, which causes Windows to load from a file on disk. This underlying functionality only supports TBitmap (BMP) format.
See the nearly-duplicate question. PNG support is a designtime feature that is converting the PNG data into an internal non-PNG and not-exactly-a-BMP-either format, used internally by MS Common Controls library. View your DFM as text, and you will see what your PNG inputs have been turned into. The other answers show you that transparency is preserved, using bitmap-color based transparency.
If you want to keep your data in PNG format, you shouldn't be using a VCL TImageList to store it, because you're going to have to do a conversion from PNG to TBitmap to actually use TImageList.

Delphi and 48x48 (or bigger) imagelists - is there a workaround?

I'm getting the system imagelist (with SHGetFileInfo and SHGFI_LARGEICON), adding two of my own icons and attaching it to a TListView.
The problem is that if the user's icon size isn't set to 32x32 (like it's set to 48x48 for example) the Delphi7 TImageList fails with an "Invalid image size" error.
Does anyone know if a workaround is available? I've tried using TPngImageList but it leads to other issues.
Also, please note that I'd like to preserve the Alpha channel of the icons. Normal 1-bit transparency is not enough, as icons tend to look ugly that way.
Thanks!
I'm not aware of any limitation on the size of images that TImageList can hold. It sounds to me that your problem is that you have icons of different sizes and you can't hold icons of different sizes in the same image list.
If you are working with icons of different sizes then you are going to need to grow the smaller ones in size. You'll have to build it up in code, using a bitmap. You fill the bitmap with pure transparent alpha channel and then blt the smaller icon onto the centre of the bitmap.
Another option would be to maintain two separate image lists but if you need to draw the icons into the same list view then I think that won't get the job done. My guess is that you'll need to grow the small icons.
For alpha, you're going to need to create the image list handle yourself because the ColorDepth property doesn't exist in D7. Because of this, a vanilla D7 TImageList simply cannot support icons with alpha channels.
You work around this limitation by calling ImageList_Create, passing ILC_COLOR32 and assigning the result to ImageList.Handle. Do this before you add any images. You'll have to populate the list at run time rather than design time, but it sounds like you are already doing that.
Here's a screen shot of a 48x48 tool button with a 32bpp icon with alpha transparency:
It's true that I made this in D2010, but my above workaround will work for D7 – I used that mechanism until quite recently with D6. I'm just showing this to prove that the image list can hold 48px icons. Since TImageList is just a wrapper around the system image list component, I believe what you are attempting should be perfectly feasible.
Just when I was about to give up this page led me to the solution:
http://delphihaven.wordpress.com/2010/09/06/custom-drawing-on-glass-2/
Apparently, if you try to add an icon that is bigger than 32x32 to a timagelist in Delphi7, the VCL will give you an "Invalid image size" error while it could simply call the himagelist API - which can easily handle it.
Here is the complete solution:
unit ImageListFix;
interface
uses CommCtrl, Graphics, ImgList;
type
TImageListFixer = class(TCustomImageList)
public
function AddIcon(Image: TIcon): Integer;
end;
implementation
function TImageListFixer.AddIcon(Image: TIcon): Integer;
begin
if Image = nil then
Result := Add(nil, nil)
else
begin
Result := ImageList_AddIcon(Handle, Image.Handle);
Change;
end;
end;
end.
And the code for adding icons to the system imagelist:
DocumentImgList:=TImageListFixer(GetSystemLargeIconsList);
IconToAdd:=TIcon.Create;
try
IconToAdd.Handle := LoadImage(0, 'c:\Ico1.ico', IMAGE_ICON, DocumentImgList.Width, DocumentImgList.Height, LR_LOADFROMFILE);
DocumentImgList.AddIcon(IconToAdd);
IconToAdd.Handle := LoadImage(0, 'c:\Ico2.ico', IMAGE_ICON, DocumentImgList.Width, DocumentImgList.Height, LR_LOADFROMFILE);
DocumentImgList.AddIcon(IconToAdd);
finally
IconToAdd.Free;
end;
TImageList raises an "Invalid image size" error under only 2 conditions:
1) The TImageList's Height or Width property is less than 1, or the Height property is greater than 32768, when the TImageList is initially created via the CreateSize() constructor (there are no such limitations imposed by the Height and Width property setters).
2) you try to add/insert a new TBitmap or TIcon whose dimensions do not fit within TImageList's internal image.

Extracting PNG images from Delphi 2009 imagelist

The TImageList of Delphi 2009 has support for PNG images by adding them in the imagelist editor. Is there any way to extract a TPngImage from a TImagelist and preserving the alpha channel?
What I want to do is actually to extract the images from one TImageList, make a disabled version of them and then add them to another TImageList. During this operation I would of course like to preserve the alpha channel of the PNG images.
I did something like this with Delphi 2006.
TImageList contains a protected method GetImages. It can be accessed using the "protected bug"
type
TGetImageImageList = class (TImageList) // Please use a better name!
end;
You can cast the imagelist to the TGetImageImageList to get to the GetImages.
begin
TGetImageList(ImageList).GetImages(index, bitmap, mask);
end;
Bitmap contains the bitmap and mask is a black and white bitmap that determines the transparant sections.
You now can change the bitmap and store it using:
function Add(Image, Mask: TBitmap): Integer;
I hope this gives you enough pointers to explore further.

Resources