在xuxiaohan的指点之下,又参考了论坛中缩放BMP图片的代码,终于可以缩放GIF图片了,高兴之余,将代码公布如下,不知还有没有更好的代码?再次感谢xuxiaohan,是他指出了GIF有Frames,让我猜到了Frames对应是TGIFImage类的属性Images,其它的就容易了,如果不点破这点,打死我也写不出来呀。有时总感觉写代码时总少点什么,看来有时间要好好看看WINDOWS基础之类的东西了,不然代码总是东抄西抄。
procedure ZoomBmp(SrcBitmap, DestBitmap: TBitmap; const NewWidth, NewHeight: Integer);
var
Bmp: TBitmap;
begin
Bmp := TBitmap.Create;
try
Bmp.Width := NewWidth;
Bmp.Height := NewHeight;
SetStretchBltMode(Bmp.Canvas.Handle, HALFTONE); //加此句效果好
StretchBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height,
SrcBitmap.Canvas.Handle, 0, 0, SrcBitmap.Width, SrcBitmap.Height, SRCCOPY);
DestBitmap.Assign(Bmp);
finally
if Assigned(Bmp) then
Bmp.Free;
end;
end;
procedure TfrmEditData_Maindb.lvImageFileNamesChange(Sender: TObject;
Item: TListItem; Change: TItemChange);
var
FileName: string;
AGIFImage: TGIFImage;
i: Integer;
OldBmp, NewBmp: TBitmap;
NewWidth, NewHeight: Integer;
begin
if Item <> nil then
begin
FileName := clsGlobe.Globe.ImageDir + FArrImageInfo[Item.Index].FileName;
imgOldImage.Picture.LoadFromFile(FileName);
lblImageSize.Caption := Format('图片大小:宽:%d,高:%d', [imgOldImage.Picture.Width, imgOldImage.Picture.Height]);
AGIFImage := TGIFImage.Create;
try
AGIFImage.LoadFromFile(FileName);
NewWidth := round(AGIFImage.Width * 0.5);
NewHeight := round(AGIFImage.Height * 0.5);
for i := 0 to AGIFImage.Images.Count - 1 do
begin
OldBmp := AGIFImage.Images.Bitmap;
NewBmp := imgNewImage.Picture.Bitmap;
ZoomBmp(OldBmp, NewBmp, NewWidth, NewHeight);
AGIFImage.Images.Bitmap := NewBmp;
end;
AGIFImage.Width := NewWidth;
AGIFImage.Height := NewHeight;
AGIFImage.SaveToFile('c:/Temp.gif');
finally
if Assigned(AGIFImage) then
AGIFImage.Free;
end;
end;
end;