Several common problems in GDI + (7)

Source: Internet
Author: User

7. Multi-frame images

In order to catch up with the hero's play schedule for the third quarter, I decided to make a set in a week. In Episode 7 heroes, Peter's functions were all absorbed by his father. So the seventh episode of my series is about some features that did not fully implement by GDI +.

A multi-frame image is an image that contains multiple frames. There are not many formats that support multiple frames. Only TIFF and GIF images are supported. Other formats cannot be used as multi-frame image storage. Tiff supports many pages and GIF animation supports multiple frames. Multiple frames of TIFF can be generated using GDI +, but GIF animation cannot be generated, probably because of patents. First, let's take a look at how to generate multi-frame tiff images.

1 Public   Void Createmultiframetiff ( String Resultimage, String Image1, Params   String [] Images)
2 {
3 // Read multiple frames, the frames 'size can be different
4 Image frame1 = Image. fromfile (image1 );
5 Int Length = Images. length;
6 Image [] Frames =   New Image [length];
7 For ( Int I =   0 ; I < Length; I ++ )
8 {
9 Frames [I] = Image. fromfile (images [I]);
10 }
11
12 // Set the first frame as the base bitmap
13 Bitmap BMP result = (Bitmap) frame1;
14
15 // Create encoder parameters with different values
16 Encoderparameters Parameters =   New Encoderparameters ( 1 );
17 Parameters. Param [ 0 ] =   New Encoderparameter (encoder. saveflag ,( Long ) Encodervalue. multiframe );
18
19 // Find tiff Codec
20 List < Imagecodecinfo > Supportedcodecs =   New List < Imagecodecinfo > ();
21 Supportedcodecs. addrange (imagecodecinfo. getimageencoders ());
22 Imagecodecinfo unzip codecinfo = Supportedcodecs. Find (
23 Delegate (Imagecodecinfo info)
24 {
25 Return Info. mimetype. Equals (
26 System. net. Mime. mediatypenames. image. Tiff,
27 Stringcomparison. ordinalignorecase );
28 }
29 );
30
31 // Save the first frame to a stream.
32 BMP result. Save (resultimage, required codecinfo, parameters );
33
34 // Save the second frame into the tiff
35 Parameters. Param [ 0 ] =   New Encoderparameter (encoder. saveflag ,( Long ) Encodervalue. framedimensionpage );
36 Foreach (Image Frame In Frames)
37 {
38 BMP result. saveadd (frame, parameters );
39 }
40
41 // Flush the stream
42 Parameters. Param [ 0 ] =   New Encoderparameter (encoder. saveflag ,( Long ) Encodervalue. Flush );
43 BMP result. saveadd (parameters );
44
45 // Dispose all resources
46 Frame1.dispose ();
47 Foreach (Image Frame In Frames)
48 {
49 Frame. Dispose ();
50 }
51 BMP result. Dispose ();
52 }

 

Here we use encoderparameters to set the image encoding parameters. First, set the multi-frame attribute (16, 17 rows) when the first frame is saved, and then get the tiff encoderparameters (20-29 rows ). Use encoderparamters and imagecodecinfo to save the first frame, and then use the saveadd method to add a new frame. Finally, the stream is flushed. It is optional because it is automatically flushed during bitmap dispose.

It is relatively simple to read multi-frame Tiff. You can directly call getframecount and selectactiveframe.CodeRow 1 gets the number of all frames and uses Row 2 to select the current frame.

1 Int Count = BMP. getframecount (framedimension. Page );
2 BMP. selectactiveframe (framedimension. Page, 1 );

 

GDI + can read GIF animation. The difference is that the parameter framedimension. Time should be used in line 2 and 2 of the Code above. Although there is no ready-made function in GDI + to generate GIF animation, it can be implemented in other ways. Nothing can be done this year, but unexpected. However, that method is too complicated and underlying. When will peter in heroes get back the capabilities, I will tell you again.

Today, we will be here first.

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.