none
Can anyone explane how this works please? RRS feed

  • Question

  • this code allows the user to produce an animated giff file, it was given to me as the answer to another question.
    but i would like to know more about how it works (at the moment i dont have a clue!)

    so if anyone including John Wein could spent some time explaining how it works to me that would be a big help thank you.





        private byte[] GifAnimation = {33, 255, 11, 78, 69, 84, 83, 67, 65, 80, 69, 50, 46, 48, 3, 1, 0, 0, 0};
        private byte[] Delay = {255, 0};
        private void button1_Click(object sender, EventArgs e)
        {
          string JpegFolder = "C:\\Users\\Public\\Pictures\\Sample Pictures";
          string GifFile = "C:\\Users\\Public\\Pictures\\MultiFrame.gif";
          string[] Files = Directory.GetFiles(JpegFolder, "*.jpg");
          MemoryStream MS = new MemoryStream();
          BinaryReader BR = new BinaryReader(MS);
          BinaryWriter BW = new BinaryWriter(new FileStream(GifFile, FileMode.Create));
          Image.FromFile(Files[0]).Save(MS, ImageFormat.Gif);
          byte[] B = MS.ToArray();
          B[10] = (byte)(B[10] & 0X78); //No global color table.
          BW.Write(B, 0, 13);
          BW.Write(GifAnimation);
          WriteGifImg(B, BW);
          for (int I = 1; I < Files.Length; I++)
          {
            MS.SetLength(0);
            Image.FromFile(Files[I]).Save(MS, ImageFormat.Gif);
            B = MS.ToArray();
            WriteGifImg(B, BW);
          }
          BW.Write(B[B.Length - 1]);
          BW.Close();
          MS.Dispose();
        }
        public void WriteGifImg(byte[] B, BinaryWriter BW)
        {
          B[785] = Delay[0]; //5 secs delay
          B[786] = Delay[1];
          B[798] = (byte)(B[798] | 0X87);
          BW.Write(B, 781, 18);
          BW.Write(B, 13, 768);
          BW.Write(B, 799, B.Length - 800);
        }

    • Edited by DARREN HANSON Monday, January 25, 2010 4:34 PM more info
    • Moved by Chao Kuo Monday, February 1, 2010 9:17 AM Beyond scope in C# (From:Visual C# General)
    Monday, January 25, 2010 4:27 PM

All replies

  • You posted the code out of context.  Asking someone to explain the Gif standard is unreasonable.  You should be able to understand the code when  you have a workable knowledge of the standard.  The Gif image container is only slightly more complex than the bitmap container.
    Monday, January 25, 2010 8:17 PM
  • I guess that is what you get when you have someone write your code for you. 

    Monday, January 25, 2010 8:27 PM
  • What gif standard?
    i dont know what you mean.


    im trying to learn im sorry if my methods are a little unusual.

    i have actulaly added open file dialog & save file dialogs to the code so i can select witch files i want to use and where to save it.

    and thats all well & good but i dont just want to copy other pls work i want to be able to do it for myself, so thats why im asking + specifically asked you as you wrote the code and therefore understand it.



    I am very new to programing so please excuse my lack of knowledge.
    Monday, January 25, 2010 8:47 PM
  • What gif standard?

    You'll need GIF89a.  Please read the line above the code in the post you copied the code from.
    Monday, January 25, 2010 8:54 PM
  • re:  Here is basic code to combine image files into an animated GIF.  You'll need the GIF specification, which can be found many places on the web.


    i have googled it but am still none the wiser, i havent found anything thats actuley called "the gif specification".
    this it what i found and am not sure how it affects me



        Cover Sheet for the GIF89a Specification


    DEFERRED CLEAR CODE IN LZW COMPRESSION

    There has been confusion about where clear codes can be found in the
    data stream. As the specification says, they may appear at anytime. There
    is not a requirement to send a clear code when the string table is full.

    It is the encoder's decision as to when the table should be cleared. When
    the table is full, the encoder can chose to use the table as is, making no
    changes to it until the encoder chooses to clear it. The encoder during
    this time sends out codes that are of the maximum Code Size.

    As we can see from the above, when the decoder's table is full, it must
    not change the table until a clear code is received. The Code Size is that
    of the maximum Code Size. Processing other than this is done normally.

    Because of a large base of decoders that do not handle the decompression in
    this manner, we ask developers of GIF encoding software to NOT implement
    this feature until at least January 1991 and later if they see that their
    particular market is not ready for it. This will give developers of GIF
    decoding software time to implement this feature and to get it into the
    hands of their clients before the decoders start "breaking" on the new
    GIF's. It is not required that encoders change their software to take
    advantage of the deferred clear code, but it is for decoders.

    APPLICATION EXTENSION BLOCK - APPLICATION IDENTIFIER

    There will be a Courtesy Directory file located on CompuServe in the PICS
    forum. This directory will contain Application Identifiers for Application
    Extension Blocks that have been used by developers of GIF applications.
    This file is intended to help keep developers that wish to create
    Application Extension Blocks from using the same Application Identifiers.
    This is not an official directory; it is for voluntary participation only
    and does not guarantee that someone will not use the same identifier.

    E-Mail can be sent to Larry Wood (forum manager of PICS) indicating the
    request for inclusion in this file with an identifier.





    I am very new to programing so please excuse my lack of knowledge.
    • Edited by DARREN HANSON Monday, January 25, 2010 11:14 PM add info
    Monday, January 25, 2010 11:08 PM
  • Wikipedia is usually the best place to search after MSDN for coding questions.  If you type "GIF" into any search engine (Yahoo, Bing, Google), one of the first entries should be

    Graphics Interchange Format


    on  wikipedia.

    If there isn't enough info there,  and you exhaust the links, repost. 
    Monday, January 25, 2010 11:35 PM
  • What gif standard?
    i dont know what you mean.


    im trying to learn im sorry if my methods are a little unusual.

    i have actulaly added open file dialog & save file dialogs to the code so i can select witch files i want to use and where to save it.

    and thats all well & good but i dont just want to copy other pls work i want to be able to do it for myself, so thats why im asking + specifically asked you as you wrote the code and therefore understand it.



    I am very new to programing so please excuse my lack of knowledge.
    The GIF is a binary standard for storing an image.  The reason that people are trying to point you to the standard is that the standard will explain what bytes have to be set where in the file in order for applications that look at the GIF to recognize it as a GIF (in this case an animated GIF).  

    The way this particular code is working is that it writes the header information to the new GIF file, then for each image that will be part of the animated GIF, it writes the image followed by some bytes that indicate (among other things) the delay before showing the next image.  If you want to understand explicitly why you have to write particular bytes to particular places in the file, you'll need to read the standard.

    Tuesday, January 26, 2010 1:42 AM