昨天的实现,效率非常低,基本要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
}
注释部分是原来代码
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

目前仍然有两个问题:
1)透明背景
2)生成的文件体积变大
望得到更多的指教