2

Right so I have a program that can edit images and whatnot. I've managed to allow the user to save as PNG files using the PNGEncoder class from as3corelib, but I'd also like to allow the user to save images as GIF files. The problem is, the only information I can find for saving GIFs is for animated GIFs (with libraries like as3gif), but I want to save them as static GIFs.

Anybody know a library or method to do this? I'm making an AIR app using the Flex 4 SDK with FlashDevelop.

EDIT: I tried making a single-framed GIF using as3gif:

var encoder:GIFEncoder = new GIFEncoder();
encoder.setRepeat(0);
encoder.setDelay(500);
encoder.start();
encoder.addFrame(image);
encoder.finish();

imageBytes = encoder.stream;

I then went on to save the bytes using a FileStream, but it gave me an invalid GIF file. I also tried Jonatan's code but that gave the same result.

EDIT: The problem was elsewhere in my code. Encoding a gif using as3gif with a single frame gives a fully functional static gif.

puggsoy
  • 1,270
  • 1
  • 11
  • 34

1 Answers1

3

Simply make a gif with just one frame, and you have a static gif.

EDIT: The code would look something like this;

var ge:GIFEncoder = new GIFEncoder();
ge.start();
ge.addFrame(myBitmapData);
ge.finish();

ge.stream; //This should now contain the image data

(untested)

Jonatan Hedborg
  • 4,382
  • 21
  • 30
  • Right, I was thinking of that but wasn't sure, I thought maybe there's some special kind of encoding for static GIFs. However you seem pretty certain so I'll check it out. If it works I'll mark this as the answer. – puggsoy Jul 29 '12 at 16:30
  • Nope, doesn't work, at least not with the as3gif library. Thanks for the suggestion though, I see your logic. – puggsoy Jul 29 '12 at 21:08
  • What doesn't work? Does it not generate valid gifs? Update the question with some code :) – Jonatan Hedborg Jul 29 '12 at 21:53
  • Yeah, it gives an invalid GIF. However I haven't tried making an animated GIF so it could be the library itself. I tried your code, and I'll update my question with some other code I tried (but didn't work). – puggsoy Jul 30 '12 at 00:31
  • Welp, after looking over my code carefully and testing it with tracing, I realised that the reason it didn't work was that my if statement that surrounded it was faulty, so the code wasn't executing at all, and it was saving an empty gif file. So yeah, it's my fault sorry, your solution works perfectly ;) – puggsoy Jul 30 '12 at 12:19
  • Did you try another library than as3gif ? I've tested it with the above answer, and I found it very slow, specially when you try to set quality under 10 – simo Oct 31 '12 at 02:42