.net c# gif动画如何添加图片水印实现思路及代码
程序员文章站
2024-03-04 16:58:47
复制代码 代码如下: public static bitmap watermarkwithtext(system.drawing.bitmap origialgif, st...
复制代码 代码如下:
public static bitmap watermarkwithtext(system.drawing.bitmap origialgif, string
text,string filepath)
{
//用于存放桢
list<frame> frames = new
list<frame>();
//如果不是gif文件,直接返回原图像
if (origialgif.rawformat.guid
!= system.drawing.imaging.imageformat.gif.guid)
{
return origialgif;
}
//如果该图像是gif文件
foreach (guid guid in
origialgif.framedimensionslist)
{
system.drawing.imaging.framedimension
framedimension = new system.drawing.imaging.framedimension(guid);
int
framecount = origialgif.getframecount(framedimension);
for (int i = 0; i
< framecount; i++)
{
if (origialgif.selectactiveframe(framedimension,
i) == 0)
{
int delay =
convert.toint32(origialgif.getpropertyitem(20736).value.getvalue(i));
image
img = image.fromhbitmap(origialgif.gethbitmap());
font font = new font(new
fontfamily("宋体"), 35.0f,fontstyle.bold);
graphics g =
graphics.fromimage(img);
g.drawstring(text, font, brushes.blanchedalmond,
new pointf(10.0f, 10.0f));
frame frame = new frame(img, delay);
frames.add(frame);
}
}
gif.components.animatedgifencoder gif =
new gif.components.animatedgifencoder();
gif.start(filepath);
gif.setdelay(100);
gif.setrepeat(0);
for (int i = 0; i <
frames.count; i++)
{
gif.addframe(frames[i].image);
}
gif.finish();
try
{
bitmap gifimg =
(bitmap)bitmap.fromfile(filepath);
return gifimg;
}
catch
{
return origialgif;
}
}
return origialgif;
}