procedure TfrmMain.btnOpenGifClick(Sender: TObject);
var
AniHeader: TAniHeader;
Width, Height, W, H: single;
DC: HDC;
Graphics: GpGraphics;
imgOUT: GPImage;
TempPath:
string;
Ext:
string;
FileOut:
string;
Result: GPStatus;
img, NewImg: GPImage;
imgW, imgH: single;
sBuffer: TBytes;
offset: dword;
FileReplaceExt:
string;
FileName:
string;
hFileOut: THandle;
size: DWORD;
dwBytesRead,
dwByttesWritten: DWORD;
begin
Result:= GenericError;
OpenDialog := TOpenDialog.Create(self);
OpenDialog.InitialDir := GetCurrentDir;
OpenDialog.Options := [ofFileMustExist];
OpenDialog.Filter := '
Animate Gifs|*.gif';
if OpenDialog.Execute
then
begin
Ext := ExtractFileExt(OpenDialog.FileName);
if Ext = '
.gif'
then
begin
imgOUT := CreateMultiFrame(PWideChar(OpenDialog.FileName), AniHeader, 512);
if Assigned(imgOUT)
then
begin
Ext := '
.png';
FileReplaceExt := StringReplace(OpenDialog.FileName, '
.gif', '
.png', [rfReplaceAll]);
FileName := ExtractFileName(FileReplaceExt);
TempPath := SysUtils.GetEnvironmentVariable('
temp');
FileOut := TempPath + '
\' + FileName;
Result := SaveToPNG(PWideChar(FileOut), imgOUT);
if Result = OK
then
begin
lblStatus.Caption := '
Status = Ok';
FileIn := PWideChar(FileOut);
if FileExists(FileIn)
then
begin
FileOut := FileReplaceExt;
FileOne := PWideChar(TempPath + '
\PNG_ani1.png');
FileTwo := PWideChar(TempPath + '
\PNG_ani2.png');
GdipLoadImageFromFile(FileIn, img);
GPStatus(GdipGetImageDimension(img, imgW, imgH));
if Assigned(img)
then
begin
DC := GetDC(0);
if GdipCreateFromHDC(
DC, Graphics) = OK
then
begin
if GdipCreateBitmapFromGraphics(AniHeader.Width, AniHeader.Height, Graphics,
NewImg) = OK
then
begin
if GdipCloneBitmapAreaI(0, 0, AniHeader.Width, AniHeader.Height,
PixelFormat32bppARGB, img, NewImg) = OK
then
begin
SaveToPNG(FileOne, NewImg);
GdipDisposeImage(NewImg);
end;
end;
if GdipCreateBitmapFromGraphics(round(imgW - AniHeader.Width), AniHeader.Height,
Graphics, NewImg) = OK
then
begin
if GdipCloneBitmapAreaI(AniHeader.Width, 0, round(imgW - AniHeader.Width),
AniHeader.Height, PixelFormat32bppARGB, img, NewImg) = OK
then
begin
SaveToPNG(FileTwo, NewImg);
GPStatus(GdipDisposeImage(NewImg));
end;
end;
GPStatus(GdipDeleteGraphics(Graphics));
end;
ReleaseDC(0,
DC);
GPStatus(GdipDisposeImage(img));
end;
if FileExists(FileOne)
then
begin
GetImageSizeFromFile(FileOne, Width, Height);
hFileIn := CreateFile(FileOne, FILE_READ_DATA, FILE_SHARE_READ,
nil, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if hFileIn <> INVALID_HANDLE_VALUE
then
begin
size := GetFileSize(hFileIn,
nil);
Setlength(sBuffer, size);
if (ReadFile(hFileIn, sBuffer[0], size, dwBytesRead,
nil))
then
begin
offset := size + 1;
CloseHandle(hFileIn);
hFileOut := CreateFile(PWideChar(FileOut), GENERIC_WRITE, 0,
nil, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, 0);
if hFileOut <> INVALID_HANDLE_VALUE
then
begin
WriteFile(hFileOut, sBuffer[0], size, dwByttesWritten,
nil);
if FileExists(FileTwo)
then
begin
GetImageSizeFromFile(FileTwo, W, H);
hFileIn := CreateFile(FileTwo, FILE_READ_DATA, FILE_SHARE_READ,
nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if hFileIn <> INVALID_HANDLE_VALUE
then
begin
size := GetFileSize(hFileIn,
nil);
Setlength(sBuffer, size);
dwBytesRead := 0;
if ReadFile(hFileIn, sBuffer[0], size, dwBytesRead,
nil)
then
begin
dwByttesWritten := 0;
WriteFile(hFileOut, sBuffer[0], size, dwByttesWritten,
nil);
AniHeader.Frame := round((W / Width)) + 1;
AniHeader.Width := round(Width);
AniHeader.Height := round(Height);
if AniHeader.Frame < 4
then
AniHeader.Speed := 75
else if AniHeader.Frame < 8
then
AniHeader.Speed := 50
else if AniHeader.Frame < 12
then
AniHeader.Speed := 37
else
AniHeader.Speed := 30;
end;
end;
AniHeader.offset := offset;
AniHeader.reserved := 0;
AniHeader.Sign := ANIM;
dwByttesWritten := 0;
WriteFile(hFileOut, AniHeader, sizeof(AniHeader), dwByttesWritten,
nil);
end;
// end FileExists(FileTwo)
CloseHandle(hFileIn);
end;
//end hFileOut <> INVALID_HANDLE_VALUE
CloseHandle(hFileOut);
end;
// end (ReadFile(hFileIn, sBuffer[0], size, dwBytesRead, nil))
end;
// end hFileIn <> INVALID_HANDLE_VALUE
end // end FileExists(FileOne)L
else
CloseHandle(hFileIn);
end;
end;
// end Result = OK
FileOut := FileOut + '
Frame count =' + IntToStr(AniHeader.Frame);
MessageBox(0, PWideChar(FileOut), '
GIFtoAPNG', 0);
DeleteFile(FileOne);
DeleteFile(FileTwo);
DeleteFile(FileIn);
end;
// end Assigned(imgOUT)
end;
// end Ext := '.gif'
end // end OpenDialog.Execute
else
begin
lblStatus.Caption := '
Status = ' + (cStatus[Result]);
end;
if AniHeader.Frame = 0
then
lblStatus.Caption := '
Status = ' + (cStatus[Result]);
OpenDialog.Free;
end;