昨天的实现,效率非常低,基本要10s左右,今天重新改良了一下gif的Encoder,效率提高了不,基本实现了gif添加水印,但透明的gif在添加水印的时候仍然存在问题,有时间再研究研究
原gif
水印之后的图片为:
但是,透明背景的却有些问题
原图
水印后
改动的部分
protected void GetImagePixels()
{
int w = image.Width;
int h = image.Height;
// int type = image.GetType().;
if ((w != width)
|| (h != height)
)
{
// create new image with right size/format
Image temp =
new Bitmap(width, height);
Graphics g = Graphics.FromImage(temp);
g.DrawImage(image, 0, 0);
image = temp;
g.Dispose();
}
/**//*
ToDo:
improve performance: use unsafe code
*/
pixels = new Byte[3 * image.Width * image.Height];
int count = 0;
Bitmap tempBitmap = new Bitmap(image);
int wh = image.Width;
int he = image.Height;
System.Drawing.Imaging.BitmapData bmpData = tempBitmap.LockBits(new Rectangle(0, 0, wh, he), System.Drawing.Imaging.ImageLockMode.ReadWrite, image.PixelFormat);
unsafe
{
byte* p = (byte*)bmpData.Scan0.ToPointer();
for (int i = 0; i < 4 * wh * he; i += 4)
{
pixels[count] = *(p + i+2);
count++;
pixels[count] = *(p + i + 1);
count++;
pixels[count] = *(p + i );
count++;
}
}
tempBitmap.UnlockBits(bmpData);
//count = 0;
//for (int th = 0; th < image.Height; th++)
//{
// for (int tw = 0; tw < image.Width; tw++)
// {
// Color color = tempBitmap.GetPixel(tw, th);
// pixels[count] = color.R;
// count++;
// pixels[count] = color.G;
// count++;
// pixels[count] = color.B;
// count++;
// }
//}
// pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
}