. NET implements clipping of GIF pictures

Source: Internet
Author: User

Related class library link: http://pan.baidu.com/s/1gd9XfBt Password: h27c

Reference Links:

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

http://cnn237111.blog.51cto.com/2359144/1261422


Toss some time,ngif can not be used directly, there will be a variety of problems, synthetic pictures have black lines and so on. Message below there are a lot of users of the message at home and abroad, there is a revision for the above to make changes. After a bit of research and a combination of related resources , I can finally meet my basic needs. Others do not repeat, the other people said.

The implementation of the principle, first a frame of the GIF picture is split into JPG pictures, and then for each picture to cut, local cutting or other than cutting and so on can own the online have a lot of jpge pictures of the processing class, I also provide a Thumbnail.cs class can be downloaded .

Implementation part

  private static size gifsize;        private  static int delay;        /// <summary>         ///  crop gif images          /// </summary>        /// <param name= " Giffilepath "> Source file Address </param>        /// <param  Name= "MaxWidth" > Max width </param>        /// <param  Name= "MaxHeight" > Maximum height </param>        /// <param  Name= "OutputPath" > Output file Address </param>        public static  void getthumbnail (string giffilepath, int maxwidth, int maxheight,  String outputpath)         {             list<image> giflist = getframes (Giffilepath);             animatedgifencoder ae = new animatedgifencoder ();             ae. Start ();             ae. Setdelay (delay);    //  delay interval              ae. Setrepeat (0);   //-1: No loop, 0: Always loop   play                //I'm here. Zoom Out (if the original length is greater than the width of the reference scale, and vice-versa with the width of the base)              size _newsize = thumbnail.resizeimage (gifSize.Width,  Gifsize.height, maxwidth, maxheight);      &nBsp;      ae. SetSize (_newsize.width,_newsize.height);             for  (int i = 0, count = giflist.count; i < count;  i++)             {                 //Image frame =  Image.FromFile (Giflist[i]);                 ae. Addframe (Giflist[i]);            }             ae. Finish ();             ae. Output (OutputPath);        }         //decoding GIF pictures          public static list<image> getframes (String ppath)          {             Image gif = image.fromfile (Ppath);             gifsize = new size (GIF. Width, gif. Height);             framedimension fd =  new framedimension (GIF. Framedimensionslist[0]);             //gets the number of frames ( GIF pictures may contain multiple frames, other format pictures generally only one frame)             int count  = gif. Getframecount (FD);           // list<string>  gifList = new List<string> ();              List<image> giflist = new list<image> ();             //saving frames in JPEG format              for  (int i = 0; i < count; i++)              {                 gif. Selectactiveframe (fd, i);                 //gets the delay time of the first frame  . /*-----Explain that, according to reason, the delay time should be obtained for each frame, and then set the delay when merging, but I test a lot of pictures of the time between each frame is the same------*/                 if  (i == 0)                  {                     for  (int j = 0; j < gif. propertyidlist.length; j++)//Traverse Frame Properties                      {                         if  (int) gif. Propertyidlist.getvalue (j)  == 0x5100)//. If the delay time                          {                              PropertyItem pItem =  (propertyitem) gif. Propertyitems.getvalue (j);//Get Delay Time Properties                              byte[] delaybyte = new byte[4];//delay Time, In 1/100-second units                              delaybyte[0] = pitem.value[i  * 4];                             delayByte[1] =  pitem.value[1 + i * 4];                              delaybyte[2] = pitem.value[2 + i * 4];                              delayByte[3] = pitem.value[3 + i * 4];                              Delay = bitconverter.toint32 (delaybyte, 0)  * 10; //times 10, gets to milliseconds                               break;                         }                     }                 }                 giflist.add ( new Bitmap (GIF) )///Here you can save each frame picture as a file GiF.save (,); On Demand             }             gif. Dispose ();            return giflist;         }

Description

AnimatedGifEncoder.cs-Getimagepixels ()

Protected void getimagepixels ()  {int w = image. Width;int h = image. Height;//int type = image. GetType ().; if  ((w != width) | |   (h != height))  {// create new image with right size/ Formatimage temp = new bitmap (width, height );                 Graphics g =  Graphics.fromimage (temp);                 //  set the drawing quality of the canvas, description: Here is the equal to zoom, can be found according to the requirements of the Thumbnail.cs in accordance with their own scaling method to modify.                 g. compositingquality = compositingquality.highquality;                 g.smoothingmode = smoothingmode.highquality;&nbsP;               g. interpolationmode = interpolationmode.highqualitybicubic;                 g.drawimage (Image, new rectangle (0, 0,  width, height),  0, 0, w, h, graphicsunit.pixel); image = temp;g. Dispose ();} /*todo:improve performance: use unsafe code */pixels = new byte [  3 * image. Width * image. height ];int count = 0; Bitmap tempbitmap = new bitmap ( image );for  (int th = 0;  th < image. height; th++) {for  (int tw = 0; tw < image. width; tw++) {color color = tempbitmap.getpixel (tw, th);p ixels[count] =  Color. r;count++;p Ixels[count] =&nbsP;color. g;count++;p Ixels[count] = color. b;count++;}}             tempbitmap.dispose ();             image. Dispose ();//pixels =  ((DataBufferByte)  image.getraster (). Getdatabuffer ()). GetData ();


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.