最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++builder - How to replace an image in a TImageCollection? - Stack Overflow

programmeradmin3浏览0评论

I'm using a TVirtualImage in a TControlList connected to a TImageCollection. I would like to be able to load an image from a TOpenPictureDialog and replace an image in the Collection. For example, replace the image at index 2 with the image from the dialog box.

I would like something like:

ImageCollection1->Images[2]->Replace(OpenPictureDialog1->FileName);

There is a Name also Logo_HD1.

I'm using a TVirtualImage in a TControlList connected to a TImageCollection. I would like to be able to load an image from a TOpenPictureDialog and replace an image in the Collection. For example, replace the image at index 2 with the image from the dialog box.

I would like something like:

ImageCollection1->Images[2]->Replace(OpenPictureDialog1->FileName);

There is a Name also Logo_HD1.

Share Improve this question edited Mar 17 at 17:33 Remy Lebeau 601k36 gold badges507 silver badges850 bronze badges asked Mar 15 at 19:41 JKofskyJKofsky 1438 bronze badges 1
  • Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Then edit your question to include your source code as a working minimal reproducible example, which can be compiled and tested by others to provide an answer faster. Please show your attempts you have tried and the problems/error messages you get from your attempts. – Progman Commented Mar 15 at 19:47
Add a comment  | 

1 Answer 1

Reset to default 1

First, you can't index the TImageCollection::Images property the way you are doing, as it is not an array property. The only reason that Images[2] even compiles is because Images is a pointer, so you are performing pointer arithmetic, but you will not be accessing the array memory correctly. You need to instead use the Images->Items sub-property, which is an indexable array property.

Second, there is no Replace() method on TImageCollectionItem. You need to instead use its SourceImages property. Which itself is another collection of items 1, where each TImageCollectionSourceItem has an Image property of type TWICImage that holds the actual image data. You can load an image file into a TWICImage object via its LoadFromFile() method.

  • 1: This means a single TImageCollection item can consist of multiple images composited together to produce the final displayed image, so be aware of that.

With that said, try this instead:

ImageCollection1->Images->Items[2]->SourceImages->Items[0]->Image->LoadFromFile(OpenPictureDialog1->FileName);

Since you know the name of the TImageCollectionItem that you want to update, you can use the TImageCollection::GetIndexByName() method to get its index within the Images collection, eg:

int index = ImageCollection1->GetIndexByName(_D("Logo_HD1"));
ImageCollection1->Images->Items[index]->SourceImages->Items[0]->Image->LoadFromFile(OpenPictureDialog1->FileName);

Lastly, after you have updated the TImageCollection, you need to Invalidate() any UI control that is displaying the Image you have updated so it will repaint itself with the new image data, eg:

VirtualImage1->Invalidate();

Or, in your example, you'll have to repaint the whole TControlList since you can't access individual items:

ControlList1->Invalidate();
发布评论

评论列表(0)

  1. 暂无评论