C # Merge multiple image files into a dynamic GIF

Source: Internet
Author: User

GIF Introduction

To merge to a GIF file, you must first understand the GIF file format. GIF was proposed by CompuServe in 1987. The gif89a standard in the official document divides GIF into many blocks. The syntax format of GIF is as follows:

<GIF Data Stream> ::=     Header <Logical Screen> <Data>* Trailer<Logical Screen> ::=      Logical Screen Descriptor [Global Color Table]<Data> ::=                <Graphic Block>  |                          <Special-Purpose Block><Graphic Block> ::=       [Graphic Control Extension] <Graphic-Rendering Block><Graphic-Rendering Block> ::=  <Table-Based Image>  |                               Plain Text Extension<Table-Based Image> ::=   Image Descriptor [Local Color Table] Image Data<Special-Purpose Block> ::=    Application Extension  |                               Comment Extension

Someone sorted out the syntax in the form of an image and it looks more intuitive:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" gif_file_stream "border =" 0 "alt =" gif_file_stream "src =" http://www.bkjia.com/uploads/allimg/131228/16024620c-0.gif "height =" 220 "/>

Many of the blocks can be repeated. Its image data module adopts the LZW algorithm, and the LZW compression algorithm is a free algorithm developed by Compuserv, however, it is strange that this algorithm suddenly becomes the patent of Unisys. According to Unisys, they have registered W part of LZW algorithm. If you want to develop programs that generate or display) image interchange format files, you need to pay the company a royalty. Unisys has caused some open-source communities to initiate the Burn all GIFs campaign to resist the use of GIF. Therefore, this stimulates CompuServe to develop the PNGPortable Network Graphics, portable Network Graphics) standard. On the one hand, it meets the needs of the market for less regulatory restrictions, on the other hand, there are even fewer technical restrictions, such as the number of colors.

Existing implementation

On CodeProject, someone has implemented the GIF image generator of C #, which has a lot of code and is also very useful. But those who are not familiar with the GIF format standards do not understand the code. It uses file streams to generate file streams based on the GIF format encoding syntax, and even implements the LZW algorithm to compress images. Therefore, this program is a good learning reference.

GDI implementation

Microsoft's GDIGraphics Device Interface) is a set of good image development class libraries, so I think it is so troublesome to create a GIF using a pure file stream, simply use the GDI method to create a GIF. Microsoft should provide this interface. So I found some sample code on the Internet. To implement dynamic GIF, you can use Image. SaveAdd to add a frame to an existing Image. However, the difficulty lies in setting the delay time. It is easy to set the latency of the first frame, but it is always ineffective for the second frame. Think twice about it.

Then I went to Stackoverflow and asked, and the reply was that Microsoft GDI does not support this GIF latency, which is also puzzling.

As a result, we had to combine local and foreign resources to modify the binary data of the generated GIF to achieve latency.

It is not complicated to use GDI to set the number of loops and code for adding frames. In C #, you can use the code to modify some GIF attributes of an image. However, if you do not know the GIF encoding format, you still cannot write it. You do not know how to assign values, this is because the official Microsoft documentation does not provide such instructions.

For example, code

PropertyItem LoopCount = img. GetPropertyItem (0x5101); // number of cycles // you can perform the following operations: BitConverter. GetBytes (loopCount); img. SetPropertyItem (LoopCount );

0x5101 is based on the document's cycle count attribute. This attribute sets the number of cycles. In the GIF document, this attribute occupies two 16 bytes in the application expansion block and is arranged in the order of low-level high. It is an unsigned integer. If 01 00 represents the hexadecimal 0x0001, if it is set to 00 00, it indicates 0, and the cycle is unlimited.

The number of cycles can be controlled by GDI, but the delay will expire after the second frame. Therefore, manually change the value.

The attribute of the delay time is in the extended block of the image control. For each frame of the image, there is a corresponding extended block of the image control, and you can modify the byte. The image control extension block starts with 21 F9. In the third and fourth byte next to it, the delay time is set, in the unit of 1% seconds. Similarly, these two bytes are arranged in a descending order. For example, For C8 00, 0x00C8 = 200, that is, the latency is 2 seconds.

Based on the above theory, you can easily write the code that sets the latency.

Byte [] bytes = File. ReadAllBytes (savefile); byte [] delaybyte = BitConverter. GetBytes (delay); // convert it to a 16-bit unsigned byte array. The array must have only two elements for (int I = 0; I <bytes. length-1; I ++) {if (bytes [I] = 0x21 & bytes [I + 1] = 0xf9) // The start sign of GraphicsControlExtension {bytes [I + 4] = delaybyte [0]; // you can modify the delay time. Bytes [I + 5] = delaybyte [1];}

Using the GDI method is relatively simple and lazy. Use the ready-made code prepared by Microsoft, and then make a slight change.

References

Http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp

Http://www.w3.org/Graphics/GIF/spec-gif89a.txt

Http://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET

Http://en.wikipedia.org/wiki/Graphics_Device_Interface

Http://www.cnblogs.com/zhengye/articles/2193006.html

Multimedia Technology basics Lin fuzong

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/1261422

Related Article

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.