using
System;
using
System.Collections.Generic;
using
System.Drawing;
using
System.IO;
using
System.Linq;
using
System.Text;
namespace
Common.Gif
{
#region Java
#endregion
public
class
AnimatedGifEncoder
{
protected
int
width;
protected
int
height;
protected
Color transparent = Color.Empty;
protected
int
transIndex;
protected
int
repeat = -1;
protected
int
delay = 0;
protected
bool
started =
false
;
protected
Stream fs;
protected
Image image;
protected
byte
[] pixels;
protected
byte
[] indexedPixels;
protected
int
colorDepth;
protected
byte
[] colorTab;
protected
bool
[] usedEntry =
new
bool
[256];
protected
int
palSize = 7;
protected
int
dispose = -1;
protected
bool
closeStream =
false
;
protected
bool
firstFrame =
true
;
protected
bool
sizeSet =
false
;
protected
int
sample = 10;
public
void
SetDelay(
int
ms)
{
delay = (
int
)Math.Round(ms / 10.0f);
}
public
void
SetDispose(
int
code)
{
if
(code >= 0)
{
dispose = code;
}
}
public
void
SetRepeat(
int
iter)
{
if
(iter >= 0)
{
repeat = iter;
}
}
public
void
SetTransparent(Color c)
{
transparent = c;
}
public
bool
AddFrame(Image im)
{
if
((im ==
null
) || !started)
{
return
false
;
}
bool
ok =
true
;
try
{
if
(!sizeSet)
{
SetSize(im.Width, im.Height);
}
image = im;
GetImagePixels();
AnalyzePixels();
if
(firstFrame)
{
WriteLSD();
WritePalette();
if
(repeat >= 0)
{
WriteNetscapeExt();
}
}
WriteGraphicCtrlExt();
WriteImageDesc();
if
(!firstFrame)
{
WritePalette();
}
WritePixels();
firstFrame =
false
;
}
catch
(IOException e)
{
ok =
false
;
}
return
ok;
}
public
bool
Finish()
{
if
(!started)
return
false
;
bool
ok =
true
;
started =
false
;
try
{
fs.WriteByte(0x3b);
fs.Flush();
if
(closeStream)
{
fs.Close();
}
}
catch
(IOException e)
{
ok =
false
;
}
transIndex = 0;
请发表评论