我用TGifImage来修改Gif的大小
虽然能正确修改图象大小,但是有一个NewGif的对像的资源却不能正确释放(会报错),搞了好久没搞定,请问哪位高手知道原因的?怎么改?
或是谁知道还有什么别的方法修改Gif 图片大小?
下面的是我用的函数,别人写的。
function PicZoom(const SourceFile, DeskFile: WideString;
nWidth, nHeight: Integer; bDelSource: WordBool): WordBool;
var
xRat, yRat: Real;
Gif: TGifImage;
newGif: TGifImage;
Bmp: TBitmap;
i, nIndex: Integer;
Stream: TMemoryStream;
Parent:TGIFSubImage;
begin
newGif := TGifImage.Create;
Bmp := TBitmap.Create;
Gif := TGifImage.Create;
Gif.LoadFromFile(SourceFile);
xRat := nWidth / Gif.Width;
yRat := nHeight / Gif.Height; try
try
//for i := 0 to Gif.Images.Count - 1 do
i:=0;
begin
Bmp.Assign(Gif.Images[i]);
Bmp.FreeImage;
if (Gif.Images[i].Width = 0) or (Gif.Images[i].Height = 0) then continue; Bmp.Width := Trunc(Int(Gif.Images[i].Bitmap.Width * xRat));
Bmp.Height := Trunc(Int(Gif.Images[i].Bitmap.Height * yRat)); Bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect, Gif.Images[i].Bitmap);
newGif.Add(Bmp); //添加GIF的扩展信息
for nIndex := 0 to Gif.Images[i].Extensions.Count - 1 do
newGif.Images[i].Extensions.Add(Gif.Images[i].Extensions.Items[nIndex]); {//begin
Stream:=TMemoryStream.Create;
Gif.Images[i].Extensions.SaveToStream(Stream);
newGif.Images[i].Extensions.LoadFromStream(Stream,newGif.Images.SubImages[i]);
Stream.Free;
//end; } newGif.Images[i].Left := Trunc(Int(Gif.Images[i].Left * xRat));;
newGif.Images[i].top := Trunc(Int(Gif.Images[i].Top * yRat));
end; newGif.Compression := gcLZW;
newGif.SaveToFile(DeskFile);
if bDelSource then
DeleteFile(SourceFile);
Result := true;
finally
FreeAndNil(Gif);
FreeAndNil(Bmp);
FreeAndNil(newGif);//这一句出错
end;
end;
except
Result := false;
end;
end;
虽然能正确修改图象大小,但是有一个NewGif的对像的资源却不能正确释放(会报错),搞了好久没搞定,请问哪位高手知道原因的?怎么改?
或是谁知道还有什么别的方法修改Gif 图片大小?
下面的是我用的函数,别人写的。
function PicZoom(const SourceFile, DeskFile: WideString;
nWidth, nHeight: Integer; bDelSource: WordBool): WordBool;
var
xRat, yRat: Real;
Gif: TGifImage;
newGif: TGifImage;
Bmp: TBitmap;
i, nIndex: Integer;
Stream: TMemoryStream;
Parent:TGIFSubImage;
begin
newGif := TGifImage.Create;
Bmp := TBitmap.Create;
Gif := TGifImage.Create;
Gif.LoadFromFile(SourceFile);
xRat := nWidth / Gif.Width;
yRat := nHeight / Gif.Height; try
try
//for i := 0 to Gif.Images.Count - 1 do
i:=0;
begin
Bmp.Assign(Gif.Images[i]);
Bmp.FreeImage;
if (Gif.Images[i].Width = 0) or (Gif.Images[i].Height = 0) then continue; Bmp.Width := Trunc(Int(Gif.Images[i].Bitmap.Width * xRat));
Bmp.Height := Trunc(Int(Gif.Images[i].Bitmap.Height * yRat)); Bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect, Gif.Images[i].Bitmap);
newGif.Add(Bmp); //添加GIF的扩展信息
for nIndex := 0 to Gif.Images[i].Extensions.Count - 1 do
newGif.Images[i].Extensions.Add(Gif.Images[i].Extensions.Items[nIndex]); {//begin
Stream:=TMemoryStream.Create;
Gif.Images[i].Extensions.SaveToStream(Stream);
newGif.Images[i].Extensions.LoadFromStream(Stream,newGif.Images.SubImages[i]);
Stream.Free;
//end; } newGif.Images[i].Left := Trunc(Int(Gif.Images[i].Left * xRat));;
newGif.Images[i].top := Trunc(Int(Gif.Images[i].Top * yRat));
end; newGif.Compression := gcLZW;
newGif.SaveToFile(DeskFile);
if bDelSource then
DeleteFile(SourceFile);
Result := true;
finally
FreeAndNil(Gif);
FreeAndNil(Bmp);
FreeAndNil(newGif);//这一句出错
end;
end;
except
Result := false;
end;
end;
nWidth, nHeight: Integer; bDelSource: WordBool): WordBool;
var
xRat, yRat: Real;
Gif: TGifImage;
newGif: TGifImage;
Bmp: TBitmap;
i, nIndex: Integer;
begin
newGif := TGifImage.Create;
Bmp := TBitmap.Create;
Gif := TGifImage.Create;
Gif.LoadFromFile(SourceFile);
xRat := nWidth / Gif.Width;
yRat := nHeight / Gif.Height; try
try
for i := 0 to Gif.Images.Count - 1 do
begin
Bmp.Assign(Gif.Images[i]);
Bmp.FreeImage;
if (Gif.Images[i].Width = 0) or (Gif.Images[i].Height = 0) then continue; Bmp.Width := Trunc(Int(Gif.Images[i].Bitmap.Width * xRat));
Bmp.Height := Trunc(Int(Gif.Images[i].Bitmap.Height * yRat)); Bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect, Gif.Images[i].Bitmap);
newGif.Add(Bmp); for nIndex := 0 to Gif.Images[i].Extensions.Count - 1 do
newGif.Images[i].Extensions.Add(Gif.Images[i].Extensions.Items[nIndex]); newGif.Images[i].Left := Trunc(Int(Gif.Images[i].Left * xRat));;
newGif.Images[i].top := Trunc(Int(Gif.Images[i].Top * yRat));
end; newGif.Compression := gcLZW;
newGif.SaveToFile(DeskFile);
if bDelSource then
DeleteFile(SourceFile);
Result := true;
finally
FreeAndNil(Gif);
FreeAndNil(Bmp);
FreeAndNil(newGif);//这句出错
end;
end;
except
Result := false;
end;
end;
FreeAndNil(newGif);//这句出错
FreeAndNil(Gif);
FreeAndNil(Bmp);
end;帮你试了,按上述顺序改一下即可,先FREE newGIF!
虽然我已经在别的地方找到这个原因了
不过还是很感谢你
同时感谢大家捧场
哈哈