【发布时间】:2014-04-08 08:50:05
【问题描述】:
我期待一种在 gif 文件的每一帧中存储 text 的方法。不打印图像中的文本,而是作为属性添加。微软有一个旧程序可以为每一帧设置文本。
如您所见,每一帧都有一个“评论”字段。
现在,我的问题是:
这个字段是 GIF 规范认可的吗?那里几乎没有文件,是这样说的。 (其实是有的)
如果是:
位于哪里?在其中一种方法中?
protected void WriteGraphicCtrlExt()
{
fs.WriteByte(0x21); // extension introducer
fs.WriteByte(0xf9); // GCE label
fs.WriteByte(4); // data block size
int transp, disp;
if (transparent == Color.Empty)
{
transp = 0;
disp = 0; // dispose = no action
}
else
{
transp = 1;
disp = 2; // force clear if using transparent color
}
//If first frame, no transparency and no dispose.
if (firstFrame)
{
disp = 0;
transp = 0;
}
else
{
if (dispose >= 0)
{
disp = dispose & 7; // user override
}
disp <<= 2;
}
// packed fields
fs.WriteByte( Convert.ToByte( 0 | // 1:3 reserved
disp | // 4:6 disposal
0 | // 7 user input - 0 = none
transp )); // 8 transparency flag
WriteShort(delay); // delay x 1/100 sec
fs.WriteByte( Convert.ToByte( transIndex)); // transparent color index
fs.WriteByte(0); // block terminator
}
protected void WriteImageDesc()
{
fs.WriteByte(0x2c); // image separator
WriteShort(0); // image position x,y = 0,0
WriteShort(0);
WriteShort(width); // image size
WriteShort(height);
// packed fields
if (firstFrame)
{
// no LCT - GCT is used for first (or only) frame
fs.WriteByte(0);
}
else
{
// specify normal LCT
fs.WriteByte( Convert.ToByte( 0x80 | // 1 local color table 1=yes
0 | // 2 interlace - 0=no
0 | // 3 sorted - 0=no
0 | // 4-5 reserved
palSize ) ); // 6-8 size of color table
}
}
编辑:
我已经找到了一种方法,就像 Hans Passand 写的那样:
protected void WriteComment(string comment)
{
fs.WriteByte(0x21);
fs.WriteByte(0xfe);
byte[] lenght = StringToByteArray(comment.Length.ToString("X"));
foreach (byte b in lenght)
{
fs.WriteByte(b);
}
WriteString(comment);
}
【问题讨论】:
-
你看过the GIF specification的第24节吗?
-
是的,已经找到方法了。
标签: c# comments gif specifications