Reducing pixelformat from TImagelist

I am using Timagelist to store a series of bitmap images which are originally pf8bit.
The Timagelist takes the format of the screen (32 bit)
I want to store these images on disk in 8bit bitmap files.
However if I try to change
  ImageList1.GetBitmap(n,Bitmap3);
  Bitmap3.PixelFormat := pf8bit;
... I lose grey scales

Thanks for your help
diver999Asked:
Who is Participating?
 
2266180Connect With a Mentor Commented:
that has nothing to do with it. I explained what is happening. re-read my explanation.

I suggest you do something like this:
- add each image that you add in teh imagelist into an object list
- when you want to save the image to file, save the one from teh object list.
do the above for every case when you have a save from the image list. (in case you have more than 1 image list).

you will define the objectlist like:

objectList:TObjectList;
and create it like:
objectList:=TObjectList.create(false);

add Contnrs to the uses clause.

then use it like:

TBitmap(objectList[index]).savetofile(filename);

add to it like: objectList.add(bitmap);
0
 
2266180Commented:
in order to maintain pixel format, or any other information except width and height, you need to set it explicitly. see this example of a proper way to put a bitmap in an imagelist and save it back to file preserving pixelformat

procedure TForm1.FormCreate(Sender: TObject);
var b:TBitmap;
    opf:TPixelFormat;
begin
  b:=TBitmap.create;
  try
    b.LoadFromFile('c:\test.bmp');
    opf:=b.PixelFormat;
    imagelist1.Height:=b.Height;
    imagelist1.Width:=b.Width;
    imagelist1.Add(b, nil);
  finally
    freeandnil(b);
  end;
  b:=TBitmap.Create;
  try
    b.PixelFormat:=opf;
    imagelist1.GetBitmap(0, b);
    b.SaveToFile('c:\test_.bmp');
  finally
    freeandnil(b);
  end;
end;
0
 
diver999Author Commented:
Thanks for the input.  <test.bmp> is 8 bit grey scale bitmap.   Display resolution is 32bit
Please look at this :
procedure TForm1.FormCreate(Sender: TObject);
var b:TBitmap;
    opf:TPixelFormat;
begin
  b:=TBitmap.create;
  try
    b.LoadFromFile('c:\test.bmp');
    opf:=b.PixelFormat;
    imagelist1.Height:=b.Height;
    imagelist1.Width:=b.Width;
    imagelist1.Add(b, nil);
  finally
    freeandnil(b);
  end;
  b:=TBitmap.Create;
  try
    b.LoadFromFile('c:\test.bmp');   //this must have the original pf
    imagelist1.GetBitmap(0, b);       //if I omit this line, test_.bmp is as test.bmp
    b.SaveToFile('c:\test_.bmp');    //if from imagelist, only one in 4 grey scales present
  finally
    freeandnil(b);
  end;
end
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
diver999Author Commented:
sorry - that should have been 1 in 8 grey scales...
0
 
2266180Commented:
well, if you look in the imagelist sources, you will notice that when you "get" a bitmpat, a new one is created and the old one is drawn onto the new one. which makes a lot of information to be lost.

so, why do you need to put the imabe in the imagelist and then save it back again? maybe there is a better solution, so give me more details on what you are trying to acomplish.
0
 
diver999Author Commented:
The original 8-bit bitmap is not loaded from a file - it is read in from an image acquisition card as part of an automatic image processing system.
Images are processed and some of them are stored in the imagelist
The user may then choose to save the images from the imagelist to a series of .bmp files.
Sometime later he may want to re-examine them by reading them back  into the bitmap object.
The grey scale reduction seems to be occuring when an image is stored by GetBitmap from the imagelist and then SaveToFile.
I think the problem is that Timagelist stores bitmaps in screen device format and getting it into a lower res object results in data loss.  
Hope this makes sense...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.