Recent

Author Topic: How to Convert a BMP file into a gif  (Read 1605 times)

Possum

  • Jr. Member
  • **
  • Posts: 69
    • uDoPage
How to Convert a BMP file into a gif
« on: October 27, 2019, 03:38:48 am »
Hi

I really though I knew how to do this. I may have years ago in Delphi..


I need to convert a BMP file into a gif file

Why does this not work

Code: Pascal  [Select][+][-]
  1.   procedure ConvertBMP2GIF;
  2. var
  3.   bmp: TBitmap;
  4.   gif: TGIFImage;
  5. begin
  6.   gif := TGifImage.Create;
  7.   try
  8.     bmp := TBitmap.Create;
  9.     try
  10.       bmp.LoadFromFile('MyBitmap.BMP');
  11.       gif.Assign(bmp);
  12.     finally
  13.       bmp.Free;
  14.     end;
  15.     gif.SaveToFile('MyBitmap.GIF');
  16.   finally
  17.     gif.Free;
  18.   end;
  19. end;

I get an exception at the savetofile command but that is due to no file being created..

Any help appreciated..

I want to convert back and forth from gif to bmp and bmp to gif..

Thank You..
« Last Edit: October 27, 2019, 03:50:25 am by Possum »

wp

  • Hero Member
  • *****
  • Posts: 7529
Re: How to Convert a BMP file into a gif
« Reply #1 on: October 27, 2019, 11:03:18 am »
fcl-image does not provide writing support for gif -- look in folder (fpc)/packages/fcl-image/src: there is a file fpreadgif, but no fpwritegif.

I don't know of any Lazarus package which addes gif writing support. You probably must port Anders Melander's/Finn Tolderlund's TGifImage (http://www.tolderlund.eu/delphi/). Or use one of the big imaging libraries such as Vampyre Imaging (http://imaginglib.sourceforge.net/ - works with Laz out of the box) or FreeImage (http://freeimage.sourceforge.net/ - not sure if it works directly with Lazarus)
« Last Edit: October 27, 2019, 11:44:07 am by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

lainz

  • Hero Member
  • *****
  • Posts: 3699
  • Leandro Diaz
Re: How to Convert a BMP file into a gif
« Reply #2 on: October 27, 2019, 02:32:39 pm »
BGRABitmap supports saving gif, see this example code:

https://forum.lazarus.freepascal.org/index.php/topic,47062.msg336312.html#msg336312

In this case is an animated gif, but I suppose just adding a single frame is enough.

Code: Pascal  [Select][+][-]
  1. var
  2.   gif : TBGRAAnimatedGif;
  3.   bmp: TBGRABitmap;
  4. begin
  5.   bmp := TBGRABitmap.Create(100,100,BGRAWhite);
  6.   bmp.FontHeight := 30;
  7.   bmp.TextRect(Rect(0,0,100,100), 'Hello', taCenter, tlCenter, BGRABlack);
  8.  
  9.   gif := TBGRAAnimatedGif.Create;
  10.   gif.SetSize(100,100);
  11.   gif.LoopCount:=100;
  12.   gif.AddFullFrame(bmp, 500);
  13.  
  14.   bmp.Fill(BGRAWhite);
  15.   bmp.TextRect(Rect(0,0,100,100), 'World', taCenter, tlCenter, BGRABlack);
  16.  
  17.   gif.AddFullFrame(bmp, 500);
  18.   gif.SaveToFile('image.gif');
  19.   gif.Free;
  20.  
  21.   bmp.Free;

Possum

  • Jr. Member
  • **
  • Posts: 69
    • uDoPage
Re: How to Convert a BMP file into a gif
« Reply #3 on: October 28, 2019, 12:36:44 am »
I didn't know PNG's have a transparent color. So I will use them.

Thx Everyone.

Maybe I should have asked how to I find out as much as I can about Lazarus and PNG files..

Cheers..

winni

  • Hero Member
  • *****
  • Posts: 1782
Re: How to Convert a BMP file into a gif
« Reply #4 on: October 28, 2019, 01:00:35 am »
Hi

There is also animation support for png files called APNG.

APNG files are shown by Firefox and Chrome.

More about APNG here:

http://littlesvr.ca/apng/


Winni

 

TinyPortal © 2005-2018