Bitmap

Hello
How to change a bitmap bits.
if I have an picture 24bit and I wont to make it 8bit and save to the hard dick.
ILPowerSoftAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
men xinConnect With a Mentor CEOCommented:
var a:Tbitmap;
begin
  a:=TBitmap.Create;
  a.LoadFromFile('c:\a.bmp');
  a.PixelFormat:=pf8bit;//convert to 8bit
  a.SaveToFile('c:\b.bmp');
  a.free;
end;

then b.bmp is a 256 colors bitmap.

menxin
0
 
karooCommented:
listening:)
0
 
simonetCommented:
Menxin,

While this is the "easy" way, you rarely get the "right" palette out of this approach.  Neither Windows nor Delphi does anything to make sure you have the "right" palette.  Since a pf24bit image can have thousands (even 100,000s) of colors, finding the "best" 236 colors for the palette is the hard part of this problem.

then you ask: But what about the other 20 (20 + 236 = 256) colors? Well, 20 colors are reserved for Windows' defalt palette and cannot be changed to a custom pallete.

Alex




0
 
kretzschmarCommented:
hi all

best result by colorreduction is with use of the jpeg-unit (jpegimage), because there is also a dithering included.

you can do it like in this code part


  MyBmp := TBitmap.Create;
  MyJPG := TJpegImage.Create;
  M := TMemoryStream.Create;
  try
    MyBmp.LoadFromFile(OpenPictureDialog1.Filename);
    MyJPG.Assign(MyBmp);
    MyJpg.SaveToStream(M);
    M.Position := 0;
    MyJpg.PixelFormat := jf8Bit;  //reduce colordepth to 8 Bit
    MyJpg.LoadFromStream(M);      //Best result if done over JPG-Object
    Image1.Picture.Bitmap.Assign(MyJPG);  //here is now the reduced bitmap
  finally
    MyBmp.Free;
    MyJpg.Free;
    M.Free;
  end;

meikl
0
All Courses

From novice to tech pro — start learning today.