以前给别人的代码:纠正8bit图到1bit时背景变红用的;有删节可能不好用// 其实最省事但是比较dirty的方法:直接开文件,seek到color table,改掉完事。 procedure TForm1.BitBtn3Click(Sender: TObject); var bitmap: TBitmap; LogPalette: TMaxLogPalette; ... begin if openpicturedialog1.Execute then try // just show it out in Image1 image1.Picture.LoadFromFile(openpicturedialog1.FileName); bitmap := TBitmap.Create; bitmap.Height := image1.Picture.Bitmap.Height; bitmap.Width := image1.Picture.Bitmap.Width; bitmap.PixelFormat := pf8bit;
// do some thing LogPalette.palVersion := $300; //fixed, see MSDN LogPalette.palNumEntries := 2; //two color, 1 bit //color1: black LogPalette.palPalEntry[0].peRed := 0; LogPalette.palPalEntry[0].peGreen := 0; LogPalette.palPalEntry[0].peBlue := 0 ; LogPalette.palPalEntry[0].peFlags := 0; //color2: white LogPalette.palPalEntry[1].peRed := 255; LogPalette.palPalEntry[1].peGreen := 255; LogPalette.palPalEntry[1].peBlue := 255; LogPalette.palPalEntry[1].peFlags := 0; bitmap.Palette := CreatePalette(PLogPalette(@LogPalette)^); bitmap.PixelFormat := pf1bit; // show the converted bitmap; image2.Picture.Bitmap.Assign(bitmap); // you may save bitmap to an file now; finally FreeAndNil(bitmap); end; end;
procedure TForm1.BitBtn3Click(Sender: TObject);
var
bitmap: TBitmap;
LogPalette: TMaxLogPalette;
...
begin
if openpicturedialog1.Execute then
try
// just show it out in Image1
image1.Picture.LoadFromFile(openpicturedialog1.FileName);
bitmap := TBitmap.Create;
bitmap.Height := image1.Picture.Bitmap.Height;
bitmap.Width := image1.Picture.Bitmap.Width;
bitmap.PixelFormat := pf8bit;
// do some thing LogPalette.palVersion := $300; //fixed, see MSDN
LogPalette.palNumEntries := 2; //two color, 1 bit
//color1: black
LogPalette.palPalEntry[0].peRed := 0;
LogPalette.palPalEntry[0].peGreen := 0;
LogPalette.palPalEntry[0].peBlue := 0 ;
LogPalette.palPalEntry[0].peFlags := 0;
//color2: white
LogPalette.palPalEntry[1].peRed := 255;
LogPalette.palPalEntry[1].peGreen := 255;
LogPalette.palPalEntry[1].peBlue := 255;
LogPalette.palPalEntry[1].peFlags := 0;
bitmap.Palette := CreatePalette(PLogPalette(@LogPalette)^);
bitmap.PixelFormat := pf1bit; // show the converted bitmap;
image2.Picture.Bitmap.Assign(bitmap);
// you may save bitmap to an file now;
finally
FreeAndNil(bitmap);
end;
end;
Image1.Picture.Bitmap.Canvas.FillRect(Rect(0,0,bmp宽度,bmp高度));不就可以了?