From: "Harm" Subject: Re: Convert BITMAP from 24Bit to 8Bit. Date: 28 Mar 2000 00:00:00 GMT Message-ID: <8bql7r$cmt12@bornews.borland.com> References: <8bpubr$5la9@bornews.borland.com> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 Organization: Another Netscape Collabra Server User X-MSMail-Priority: Normal Newsgroups: borland.public.delphi.graphics Here's a method I use that does a fine job. It's from (I believe) Davie Reed, posted many months ago in this NG. It uses Jpeg. You'll need a TImage, an OpenPicture and SavePicture Dialog, and two TButtons. Set the TImage to Autosize true. I gave the buttons long names that describe what they do... I use two TBitmaps - one is 24bit and one is 8bit. The open picture routine opens any TPicture and copies it into the 24-bit bitmap. The convert/save button uses a temporary TJPEGImage to do color reduction, then copies that into the 8-bit bitmap. It gives you an opportunity to save it, make sure you specify filename.bmp (or limit the save dialog to type bmp). For clarity of code, no error checking is done. unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, jpeg, StdCtrls, ExtDlgs, ExtCtrls; type TForm1 = class(TForm) Image1: TImage; OpenPictureDialog1: TOpenPictureDialog; OpenPictureButton: TButton; ConvertAndSave8BitBitmapButton: TButton; SavePictureDialog1: TSavePictureDialog; procedure OpenPictureButtonClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure ConvertAndSave8BitBitmapButtonClick(Sender: TObject); private { Private declarations } bmp24bit : TBitmap; bmp8bit : TBitmap; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.OpenPictureButtonClick(Sender: TObject); begin if OpenPictureDialog1.Execute then begin Image1.Picture.LoadFromFile(OpenPictureDialog1.Filename); bmp24bit.Width := Image1.Picture.Width; bmp24bit.Height := Image1.Picture.Height; bmp24bit.PixelFormat := pf24bit; bmp24bit.Canvas.Draw(0,0,Image1.Picture.Graphic); end; end; procedure TForm1.FormCreate(Sender: TObject); begin bmp24bit := TBitmap.Create; bmp8bit := TBitmap.Create; end; procedure TForm1.FormDestroy(Sender: TObject); begin bmp24bit.Free; bmp8bit.Free; end; procedure TForm1.ConvertAndSave8BitBitmapButtonClick(Sender: TObject); var tempjpeg : TJPEGImage; begin if Image1.Picture <> nil then begin bmp8bit.Width := bmp24bit.Width; bmp8bit.Height := bmp24bit.height; bmp8bit.PixelFormat := pf8bit; tempjpeg := TJPEGImage.Create; tempjpeg.CompressionQuality := 100; tempjpeg.Assign(bmp24bit); tempjpeg.JPEGNeeded; tempjpeg.PixelFormat:=jf8bit; tempjpeg.DibNeeded; bmp8bit.Assign(tempjpeg); tempjpeg.Free; Image1.Picture.Assign(bmp8bit); Image1.Invalidate; if SavePictureDialog1.Execute then bmp8bit.SaveToFile(SavePictureDialog1.FileName); end; end; end. You are welcome. http://www.users.uswest.net/~sharman1/ Cool Graphics, for free! "Samson Fu" wrote in message news:8bpubr$5la9@bornews.borland.com... > I set a TBitmap to pf8bit. However, the Image is totally different with > original one. > Seems Delphi cannot convert bit-palettes in a smart way, or I set something > wrong? > > Can anyone suggest a good and pertty way to convert 24 bit image to 8 bit? > > Thank you. > > Samson Fu