Make a Border Or Do a Crop: TBGRABitmap might be the solution lazarus / 39-Graphics ---------------------------------------------------------------------- Boleeman · 2024-08-05 13:54 Made a program to Make a Border Or Do a Crop   (Do a Crop and Not Do a Crap, Ha Ha.) Set the Border size with a TSpinedit  or Set the Crop decrease in size with that same TSpinedit Clicking on the filename in the TFilelistbox loads the picture file. Works for Bmp, png, jpg BUT NOT GiF saved back to GIF, unless I convert the gif to png. Problem: Using FPWriteGIF  from https://www.gocher.me/FPWriteGIF  gives an Access Violation Error when Adding a Border or Cropping a Gif file. Not sure why FPWriteGIF is making the Access Violation Error and how do I get rid of that error. I played around with some FPWriteGIF code by KodeZerg and successfully converted from bmp to gif in another sample code shown below without any Access Violation Error, but I was unable to get rid of that error in my own Border/Cropping program. I wonder if anyone can help out? Not sure if Bgrabmp might work better in opening and saving to different picture formats? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,  fpImage, fpWriteGif; type  TMyGifImage = class(TGifImage)  protected    class function GetWriterClass: TFPCustomImageWriterClass; override;  end;   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} class function TMyGifImage.GetWriterClass: TFPCustomImageWriterClass;begin  Result := TFPWriterGIF;end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  bmp: TBitmap;  gif: TMyGifImage;begin  bmp := TBitmap.Create;  try    bmp.LoadfromFile('test.bmp');    gif := TMyGifImage.Create;    try      gif.Assign(bmp);      gif.SaveToFile('test.gif');    finally      gif.Free;    end;  finally    bmp.Free;  end;end; initialization  TPicture.RegisterFileFormat('.gif', 'gif', TMyGifImage); end. ---------------------------------------------------------------------- marcov · 2024-08-05 14:53 Do you pass it a 256 color paletted image  ? ---------------------------------------------------------------------- Boleeman · 2024-08-06 10:17 I made a simple test gif in MSPaint. Not sure of the palette size, but that gave Access Violation error as well. The only difference I can see in KodeZerg's modifed example that works is that the BMP is freed. Perhaps something like that needs to be done in my program for the FPWriteGIF part of the code ? ---------------------------------------------------------------------- Boleeman · 2024-08-06 11:40 I might just remove that FPWriteGIF code, as it seems it might be a bit buggy to use. ---------------------------------------------------------------------- circular · 2024-08-06 11:52 BGRABitmap has its own implementation of the GIF format. So you may be more lucky. Note that if editing increases the color count over 256, you will need to set up the color quantizer as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses BGRAPalette, BGRAColorQuantization, ...;begin  ...  BGRAPalette.BGRAColorQuantizerFactory:= TBGRAColorQuantizer; ---------------------------------------------------------------------- Boleeman · 2024-08-06 11:53 Actually found a comment by WP here https://forum.lazarus.freepascal.org/index.php?topic=55818.0 The gif writer unit seems to have an issue with memory management because the demo crashes in the local ClearMappings procedure - knowing that this could create a memory leak Also found a comment by Jurassic Pork: to save bitmap in gif format you can use  vampyreImaging library (available on package manager). Thanks for that reply circular Looks like you were posting at the same time as I was replying. Would it be possible for you to provide a very simple example of how to save say a bmp or a png to simple gif using BgraBmp ? ---------------------------------------------------------------------- circular · 2024-08-06 12:51 Hi Boleeman, Sure, here is how to load a PNG and save it as GIF: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses BGRABitmap, BGRAAnimatedGif, BGRAPalette, BGRAColorQuantization;var bmp: TBGRABitmap;begin  // global setup for quantization  BGRAPalette.BGRAColorQuantizerFactory:= TBGRAColorQuantizer;   bmp := TBGRABitmap.Create('test.png');  bmp.SaveToFile('test.gif'); // normally this will use the writer of BGRAAnimatedGif  bmp.Free;end. ---------------------------------------------------------------------- Boleeman · 2024-08-06 13:17 Thank you Circular for the test code. I seem to get an error. The Image Writer is not registered for this Image Format Also I could not find TBGRABitmap.RegisterFileFormat  like for the FPWriteGIF initialization   TPicture.RegisterFileFormat('.gif', 'gif', TMyGifImage); ---------------------------------------------------------------------- circular · 2024-08-07 13:22 Ah ok. Maybe you need to add BGRAAnimatedGif in the unit clause. This is the unit that registers the format. ---------------------------------------------------------------------- Boleeman · 2024-08-07 13:30 Yes that was it. Thanks Circular. ---------------------------------------------------------------------- circular · 2024-08-07 18:45 You're welcome.  Glad you got what you're looking for.