Recent

Author Topic: List of Bitmaps to Animated Gif Class  (Read 1734 times)

DreamVB

  • Full Member
  • ***
  • Posts: 100
    • Memo Pad
List of Bitmaps to Animated Gif Class
« on: October 23, 2023, 11:39:20 am »
Hi here is a small class and example I made that converts a bunch of bitmap files 256 color to an animated GIF and saves to a file, Note you need to add BGRABitmap package to your project you can find this in the online package manager.

Class File
Code: Pascal  [Select][+][-]
  1. unit dvbMakeGif;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, BGRABitmap, BGRABitmapTypes, BGRAAnimatedGif;
  9.  
  10. type
  11.   TMakeGif = class
  12.   private
  13.     iWidth, iHeight, iLoopCount, iDelay: integer;
  14.     FileNames: TStringList;
  15.   public
  16.     constructor Create; overload;
  17.     procedure SaveGif(Filename: string);
  18.     procedure AddFile(Filename: string);
  19.     procedure FreeObj;
  20.     property LoopCount: integer read iLoopCount write iLoopCount;
  21.     property ImageWidth: integer read iWidth write iWidth;
  22.     property ImageHeight: integer read iHeight write iHeight;
  23.     property Delay: integer read iDelay write iDelay;
  24.   end;
  25.  
  26. implementation
  27.  
  28. constructor TMakeGif.Create;
  29. begin
  30.   FileNames := TStringList.Create;
  31. end;
  32.  
  33. procedure TMakeGif.AddFile(Filename: string);
  34. begin
  35.   //Add bitmap file to string list
  36.   if FileExists(Filename) then
  37.   begin
  38.     //Only add files that are found
  39.     FileNames.Add(Filename);
  40.   end;
  41. end;
  42.  
  43. procedure TMakeGif.SaveGif(Filename: string);
  44. var
  45.   gif: TBGRAAnimatedGif;
  46.   bmp: TBGRABitmap;
  47.   X: integer;
  48. begin
  49.   //Works with 256 color bitmaps
  50.   gif := TBGRAAnimatedGif.Create;
  51.   try
  52.     //Set loop count how mnay times it plays
  53.     gif.LoopCount := LoopCount;
  54.     //Set the gif image width and height
  55.     gif.SetSize(ImageWidth, ImageHeight);
  56.     gif.OptimizeFrames;
  57.  
  58.     for X := 0 to Filenames.Count - 1 do
  59.     begin
  60.       //Get bitmap from filename
  61.       bmp := TBGRABitmap.Create(Filenames[X]);
  62.       try
  63.         //Add bitmap as gif frame and add delay in milliseconds
  64.         gif.AddFullFrame(bmp, Delay);
  65.       finally
  66.         //Free bmp obj
  67.         bmp.Free;
  68.       end;
  69.     end;
  70.     //Save the gif file.
  71.     gif.SaveToFile(Filename);
  72.   finally
  73.     //Free gif obj
  74.     gif.Free;
  75.   end;
  76. end;
  77.  
  78. procedure TMakeGif.FreeObj;
  79. begin
  80.   //Clear and destroy the string list
  81.   FreeAndNil(FileNames);
  82. end;
  83. end.
  84.  

Example

Code: Pascal  [Select][+][-]
  1. procedure TFrmMain.cmdMakeGifClick(Sender: TObject);
  2. var
  3.   gif: TMakeGif;
  4. begin
  5.   // How to use this class add dvbMakeGif to uses
  6.   //Create the object
  7.   gif := TMakeGif.Create;
  8.   //Important make sure the width and height of the images are correct size
  9.   gif.ImageWidth := 905;
  10.   gif.ImageHeight := 648;
  11.   gif.LoopCount := 0; // Set to 0 to play forever;
  12.   gif.Delay := 1000;
  13.   //Add the images
  14.   gif.AddFile('c:\out\source\1.bmp');
  15.   gif.AddFile('c:\out\source\2.bmp');
  16.   gif.AddFile('c:\out\source\3.bmp');
  17.   gif.AddFile('c:\out\source\4.bmp');
  18.   gif.SaveGif('c:\out\source\output\test.gif');
  19.   gif.FreeObj;
  20. end;
  21.  
  22. Hope you find the code us full note this was done quick so there maybe room for improvements
  23.  
  24.  
Dream Believe Achieve

 

TinyPortal © 2005-2018