改善了性能的gif動畫添加水印
昨天的實現,效率非常低,基本要10s左右,今天重新改良了一下gif的Encoder,效率提高了不,基本實現了gif添加水印,但透明的gif在添加水印的時候仍然存在問題,有時間再研究研究
原gif
水印之后的圖片為:
但是,透明背景的卻有些問題
原圖
水印后
改動的部分
1
protected void GetImagePixels()
2
{
3
int w = image.Width;
4
int h = image.Height;
5
// int type = image.GetType().;
6
if ((w != width)
7
|| (h != height)
8
)
9
{
10
// create new image with right size/format
11
Image temp =
12
new Bitmap(width, height);
13
Graphics g = Graphics.FromImage(temp);
14
g.DrawImage(image, 0, 0);
15
image = temp;
16
g.Dispose();
17
}
18
/*
19
ToDo:
20
improve performance: use unsafe code
21
*/
22
pixels = new Byte[3 * image.Width * image.Height];
23
int count = 0;
24
Bitmap tempBitmap = new Bitmap(image);
25
int wh = image.Width;
26
int he = image.Height;
27
System.Drawing.Imaging.BitmapData bmpData = tempBitmap.LockBits(new Rectangle(0, 0, wh, he), System.Drawing.Imaging.ImageLockMode.ReadWrite, image.PixelFormat);
28
unsafe
29
{
30
byte* p = (byte*)bmpData.Scan0.ToPointer();
31
for (int i = 0; i < 4 * wh * he; i += 4)
32
{
33
pixels[count] = *(p + i+2);
34
count++;
35
pixels[count] = *(p + i + 1);
36
count++;
37
pixels[count] = *(p + i );
38
count++;
39
}
40
}
41
tempBitmap.UnlockBits(bmpData);
42
//count = 0;
43
//for (int th = 0; th < image.Height; th++)
44
//{
45
// for (int tw = 0; tw < image.Width; tw++)
46
// {
47
// Color color = tempBitmap.GetPixel(tw, th);
48
// pixels[count] = color.R;
49
// count++;
50
// pixels[count] = color.G;
51
// count++;
52
// pixels[count] = color.B;
53
// count++;
54
// }
55
//}
56
57
// pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
58
}
注釋部分是原來代碼
protected void GetImagePixels()2
{3
int w = image.Width;4
int h = image.Height;5
// int type = image.GetType().;6
if ((w != width)7
|| (h != height)8
)9
{10
// create new image with right size/format11
Image temp =12
new Bitmap(width, height);13
Graphics g = Graphics.FromImage(temp);14
g.DrawImage(image, 0, 0);15
image = temp;16
g.Dispose();17
}18
/*19
ToDo:20
improve performance: use unsafe code 21
*/22
pixels = new Byte[3 * image.Width * image.Height];23
int count = 0;24
Bitmap tempBitmap = new Bitmap(image);25
int wh = image.Width;26
int he = image.Height;27
System.Drawing.Imaging.BitmapData bmpData = tempBitmap.LockBits(new Rectangle(0, 0, wh, he), System.Drawing.Imaging.ImageLockMode.ReadWrite, image.PixelFormat);28
unsafe29
{30
byte* p = (byte*)bmpData.Scan0.ToPointer();31
for (int i = 0; i < 4 * wh * he; i += 4)32
{33
pixels[count] = *(p + i+2);34
count++;35
pixels[count] = *(p + i + 1);36
count++;37
pixels[count] = *(p + i );38
count++;39
}40
}41
tempBitmap.UnlockBits(bmpData);42
//count = 0;43
//for (int th = 0; th < image.Height; th++)44
//{45
// for (int tw = 0; tw < image.Width; tw++)46
// {47
// Color color = tempBitmap.GetPixel(tw, th);48
// pixels[count] = color.R;49
// count++;50
// pixels[count] = color.G;51
// count++;52
// pixels[count] = color.B;53
// count++;54
// }55
//}56

57
// pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();58
}目前仍然有兩個問題:
1)透明背景
2)生成的文件體積變大
望得到更多的指教
作者:jillzhang
出處:http://jillzhang.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
出處:http://jillzhang.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


浙公網安備 33010602011771號