GIFIMAGE使用中将一个GIF彩色图转换成黑白图的问题(50分)

  • 主题发起人 主题发起人 liliacs
  • 开始时间 开始时间
L

liliacs

Unregistered / Unconfirmed
GUEST, unregistred user!
这样写是可以转换的
(1)image1.Canvas.Draw(0,0,gif.Images.SubImages[0].bitmap);
(2)GIFImageDefaultColorReduction := rmMonochrome;
GIFImageDefaultDitherMode := dmNearest;
GIF2 := TGIFImage.Create;
GIF2.Assign(image1.Picture);
Image2.Picture.Assign(GIF2);
如果将(1)象以下这样写
image1.Picture.Bitmap:=gif.Images.SubImages[1].Bitmap;
或 image1.Picture.Assign(Gif.Images.SubImages[0].Bitmap);
就不可转换了,请问为什么?我想用后面的写法,不想用CANVAS,同时用GIFIMAGE实现,可以吗?
 
[:D]已经自行解决,贴上代码给将会遇到相似问题的朋友。

var
Bitmap : TBitmap;
NewGIF : TGIFImage;
NewBitmap : TBitmap;
i : integer;
begin
// Reduce to the Netscape 216 color palette
NewGIF := TGIFImage.Create;
try
Bitmap := TBitmap.Create;
try

for i := 0 to Panel1.ControlCount-1 do
if (Panel1.Controls is TImage) and (TImage(Panel1.Controls).Picture.Graphic <> nil) then
begin
// Convert source image to bitmap
Bitmap.Assign(TImage(Panel1.Controls).Picture.Graphic);

// Reduce colors
NewBitmap := ReduceColors(Bitmap, rmMonochrome, dmFloydSteinberg, 0, 0);
try
// Convert reduced bitmap to GIF
NewGIF.Assign(NewBitmap);
NewGIF.SaveToFile(format('gif%d.gif', ));

// Display result
(Panel2.Controls as TImage).Picture.Assign(NewGIF);
finally
NewBitmap.Free;
end;
end;
finally
Bitmap.Free;
end;

finally
NewGIF.Free;
end;
 
顶一个。
再附上一个不错的转换方法:
procedure TForm1.Monochrome(Icon: TIcon);
var
Bitmap, Mask: TBitmap;
x, y: integer;
Color: Longint;
R, G, B, Gr: byte;
ImageList: TImageList;
begin
Bitmap := TBitmap.Create;
Mask := TBitmap.Create;
ImageList := TImageList.CreateSize(32, 32);
ImageList.AddIcon(Icon);
ImageList.GetBitmap(0, Bitmap);//以上三行实现Ico到Bitmap的转换
with Bitmap.Canvas do
for x := Cliprect.Left to Cliprect.Right do
for y := Cliprect.Top to Cliprect.Bottom do
begin
Color := ColorToRGB(Pixels[x, y]);
B := (Color and $FF0000) shr 16;
G := (Color and $FF00) shr 8;
R := (Color and $FF);
Gr := Trunc(R*0.19 + G*0.44 + B*0.37);
Pixels[x, y] := Gr shl 16 or Gr shl 8 or Gr;
end;//以上实现黑白图象
Mask.Assign(Bitmap);
Mask.Mask(clWhite);//为透明图象设置掩码位图
ImageList.Height := Bitmap.Height;
ImageList.Width := Bitmap.Width;
ImageList.Clear;
ImageList.Add(Bitmap, Mask);
ImageList.GetIcon(0, Icon);//以上实现Bitmap到Ico的转换
ImageList.Free;
Bitmap.Free;
Mask.Free;
end;
 
后退
顶部