Add watermarks to gif animations with improved performance

Source: Internet
Author: User

Yesterday's implementation was very inefficient. It took about 10 seconds. Today, I improved the GIF encoder again, but the efficiency was not improved. Basically, I added watermarks to gif images, however, a transparent GIF still has problems when adding watermarks, so we have time to study it again.
Original GIF

The image after the watermark is:

However, there are some problems with the transparent background.
Source image

After watermark

Modified part

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 /**/ /*
19Todo:
20Improve 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 BMP data = Tempbitmap. lockbits ( New Rectangle ( 0 , 0 , Wh, He), system. Drawing. imaging. imagelockmode. readwrite, image. pixelformat );
28 Unsafe
29 {
30 Byte * P = ( Byte * ) BMP data. 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 (BMP data );
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 }

Comments are originalCode
There are still two problems:
1) Transparent background
2) The size of the generated file increases.
Hope for more advice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.