Link to home
Start Free TrialLog in
Avatar of ZifNab
ZifNab

asked on

How reduce amount of stored bitmaps

Hi,

 I've a form with dozens of one same component... made by myself in the early D1 period. Now it's time to alter it...

Ok, what it does : It's a sort of range visualizer. You give two values one limit for above, one for below.

If a given data-value (db or not db) is in the selected range, then a certain bitmap is shown, if not, another bitmap is shown. Now, every single component stored these bitmaps himself! Not so good, but heck it worked then.

Now, I want to do the following :

 - put an image container on the form (like image list, or PicClip of RxLib) I prefer to use PicClip, because ImageList seems to have some resource leaks.
 - put a component on the form and let this one point to a certain Image container (selectable in design-time)

 --> this way I avoid storing multible same bitmaps.

 --> But how can you do this? How can I let one component refer to another one, so that I can share one component with several others..., without storing this shared component in every component which uses it!

Is this the only way, but then I still create a TPicClip for every component and that's just what I don't want!! :

 unit ShowRange;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  PicClip;

type
  TShowRange = class(TCustomControl)
  private
    { Private declarations }
    fPicClip : TPicCLip;
    procedure SetPicClip(vPicClip : TPicClip);
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
   property PicClip: TPicClip read fPicClip write SetPicClip;
  end;

procedure Register;

implementation

procedure TShowRange.SetPicClip(vPicClip: TPicClip);
begin
 if vPicClip <> fPicClip then begin
  fPicClip.Assign(vPicClip);
 end;
end;

procedure Register;
begin
  RegisterComponents('MyComponents', [TShowRange]);
end;

end.

 
Example please!

Regards, Zif.


ASKER CERTIFIED SOLUTION
Avatar of chrismo
chrismo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Madshi
Madshi

chrismo, you're right.

ZifNab, and don't forget to hook the TPicClip changes. I don't know TPicClip, so I can't tell you how you can do this. But if you don't hook the changes your little component will crash when you remove the picClip without deleting the reference in your component before.

Regards, Madshi.
Avatar of ZifNab

ASKER

Hi all,

Hmm, very simple, ... thanks.

thanks for the replies. Now, If I use ImageList for example... how should hook then work?

Regards, Zif.
ZifNab,

look at the method "RegisterChanges" and the event "OnChange". In the event handler you should check if the imageList your component is referencing still exists and if the image index still exists. If not, your component should remove the reference to that imageList.
I've no sources, but I think the delphi help explains it good enough...

Regards, Madshi.
An option which you have is to pass the index of a specific bitmap to your bitmap lister, and let that return a bitmap handle to you based on that index value. You could "store" all your images in a TList or something similar then. Once you have the bitmap handle you can do quite a lot with the image.... You said you did not want to have to copy anything accross, and this may be an alternative.
ellessar,

each and every bitmap handle consumes windows system resources. Don't forget this. ImageList only consumes one handle per list.

Regards, Madshi.
Just goes to show, we learn something new every day... I
never realised that the ImageList is actually exposing
a basic windows image list object. That might also
explain the apparent memory leaks they are experiencing.

The windows help listing says the following:

An image list is a collection of same-sized images,
each of which can be referred to by its index. Image
lists are used to efficiently manage large sets of
icons or bitmaps. All images in an image list are
contained in a single, wide bitmap in screen device
format. An image list may also include a monochrome
bitmap that contains masks used to draw images
transparently (icon style).


ellessar,

have you ever experienced memory leaks with ImageLists? I'm working with them and never noticed something like that. Perhaps I just didn't look close enough...

Regards, Madshi.
Avatar of ZifNab

ASKER

Madshi,

 Well, I don't know anymore if TImageList has a resource leak, I thought I read about it somewhere, but I can't find it anymore. A good link to find about bugs is here http://www.dataweb.nl/~r.p.sterkenburg/bugsall.htm

regards, Zif.
Avatar of ZifNab

ASKER

mmmm, is it possible to get something like this?

A user can refer to a TPicClip OR to a TImageList?

Regards, Zif.

Avatar of ZifNab

ASKER

Thanks all.
Hey ZifNab,

I thought you was on vacation already?
Avatar of ZifNab

ASKER

I'm going ! In about 2 hours :-) Regards, Zif.