改善了性能的gif动画添加水印

博主分享了改进GIF编码器以提高水印添加效率的过程,从原本的10秒减少到更短时间,但在处理透明背景GIF时遇到难题,同时发现生成文件体积增大。寻求社区帮助解决透明背景水印和文件大小问题。

昨天的实现,效率非常低,基本要10s左右,今天重新改良了一下gif的Encoder,效率提高了不,基本实现了gif添加水印,但透明的gif在添加水印的时候仍然存在问题,有时间再研究研究
原gif

水印之后的图片为:

但是,透明背景的却有些问题
原图

水印后

改动的部分

 1None.gif  protected void GetImagePixels()
 2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 3InBlock.gif            int w = image.Width;
 4InBlock.gif            int h = image.Height;
 5InBlock.gif            //        int type = image.GetType().;
 6InBlock.gif            if ((w != width)
 7InBlock.gif                || (h != height)
 8InBlock.gif                )
 9ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
10InBlock.gif                // create new image with right size/format
11InBlock.gif                Image temp =
12InBlock.gif                    new Bitmap(width, height);
13InBlock.gif                Graphics g = Graphics.FromImage(temp);
14InBlock.gif                g.DrawImage(image, 00);
15InBlock.gif                image = temp;
16InBlock.gif                g.Dispose();
17ExpandedSubBlockEnd.gif            }

18ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*
19InBlock.gif                ToDo:
20InBlock.gif                improve performance: use unsafe code 
21ExpandedSubBlockEnd.gif            */

22InBlock.gif            pixels = new Byte[3 * image.Width * image.Height];
23InBlock.gif            int count = 0;
24InBlock.gif            Bitmap tempBitmap = new Bitmap(image);
25InBlock.gif            int wh = image.Width;
26InBlock.gif            int he = image.Height;
27InBlock.gif            System.Drawing.Imaging.BitmapData bmpData = tempBitmap.LockBits(new Rectangle(00, wh, he), System.Drawing.Imaging.ImageLockMode.ReadWrite, image.PixelFormat);
28InBlock.gif            unsafe
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif                byte* p = (byte*)bmpData.Scan0.ToPointer();
31InBlock.gif                for (int i = 0; i < 4 * wh * he; i += 4)
32ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
33InBlock.gif                    pixels[count] = *(p + i+2);
34InBlock.gif                    count++;
35InBlock.gif                    pixels[count] = *(p + i + 1);
36InBlock.gif                    count++;
37InBlock.gif                    pixels[count] = *(p + i );
38InBlock.gif                    count++;
39ExpandedSubBlockEnd.gif                }

40ExpandedSubBlockEnd.gif            }

41InBlock.gif            tempBitmap.UnlockBits(bmpData);
42InBlock.gif            //count = 0;
43InBlock.gif            //for (int th = 0; th < image.Height; th++)
44InBlock.gif            //{
45InBlock.gif            //    for (int tw = 0; tw < image.Width; tw++)
46InBlock.gif            //    {
47InBlock.gif            //        Color color = tempBitmap.GetPixel(tw, th);
48InBlock.gif            //        pixels[count] = color.R;
49InBlock.gif            //        count++;
50InBlock.gif            //        pixels[count] = color.G;
51InBlock.gif            //        count++;
52InBlock.gif            //        pixels[count] = color.B;
53InBlock.gif            //        count++;
54InBlock.gif            //    }
55InBlock.gif            //}
56InBlock.gif
57InBlock.gif            //        pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
58ExpandedBlockEnd.gif        }
注释部分是原来代码
目前仍然有两个问题:
1)透明背景
2)生成的文件体积变大
望得到更多的指教
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值