Jump to content
softtouch

How to get image dimension from images in TImageCollection?

Recommended Posts

I have many images in a TImageCollection, all have different dimensions. because of the different dimensions, I cannot use a virtualimagelist with the collection, images would get scaled.

I will draw the images manually when needed. Thats no problem, but how can I get the width and height of an image in the imagecollection? All the procedure/functions like GetBitmap etc. are all requesting a width and height and will scale the original image too.

Share this post


Link to post

TImageCollection holds several Images, where each can have multiple sizes, which renders THE size of an image meaningless. You need to inspect the SourceImages of the Image returned by the collection to get the available sizes. Only if the image has just one SourceImage there also is only one size.

Share this post


Link to post

@softtouch

 

you can try some like this

type
  TForm1 = class(TForm)
    ImageCollection1: TImageCollection;
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  System.TypInfo;

type
  ThelpToTWICImageFormat = record helper for TWICImageFormat
    function ToString: string;
  end;

  { ThelpToTWICImageFormat }

function ThelpToTWICImageFormat.ToString: string;
begin
  result := GetEnumName(typeinfo(TWICImageFormat), ord(self)).Remove(0, 3);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  LICItem  : TImageCollectionItem;
  LImage   : TWICImage;
  LText    : string;
  LImgCount: integer;
begin
  LText     := '';
  LImgCount := ImageCollection1.Count;
  //
  for var i: integer := 0 to (LImgCount - 1) do
    begin
      LICItem := ImageCollection1.Images[i];
      LText   := LText + Format('Item (names): %s, sub-images: %d', [LICItem.Name, LICItem.SourceImages.Count]);
      //
      for var j: integer := 0 to LICItem.SourceImages.Count - 1 do
        begin
          LImage := LICItem.SourceImages.Items[j].Image;
          LText  := LText + slinebreak + Format('....Sub-Image: %d W=%d, H=%d, format: %s', { }
            [j, LImage.Width, LImage.Height, LImage.ImageFormat.ToString]);
        end;
      //
      if (i < LImgCount) then
        LText := LText + slinebreak + slinebreak;
    end;
  //
  if LText.IsEmpty then
    LText := 'no images found';
  //
  Memo1.Text := LText;
end;

end.

 

 

bds_lYuBxe16EZ.gif

Share this post


Link to post

I already got what I need with the simple code:

 

idx:=ImageCollection1.GetIndexByName(imagename);
if idx<>-1 then
begin
	var img:=ImageCollection1.Images[idx].SourceImages[0].Image;
	var x:=img.Width;
	var y:=img.Height;
end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×