title: Implementing the APNG Render Function
body: <p>Hey everyone,
  So, I'm currently trying the implement the <a href="https://wiki.mozilla.org/APNG_Specification" rel="noreferrer">APNG Specification</a>, but am having some trouble with the frame rendering. My function is</p>
  
  <pre><code>    private void UpdateUI()
      {
          foreach (PictureBox pb in pics)
          {
              APNGBox box = (APNGBox)pb.Tag;
              APNGLib.APNG png = box.png;
              if (box.buffer == null)
              {
                  box.buffer = new Bitmap((int)png.Width, (int)png.Height);
              }
              APNGLib.Frame f = png.GetFrame(box.frameNum);
              using (Graphics g = Graphics.FromImage(box.buffer))
              {
                  switch (f.DisposeOp)
                  {
                      case APNGLib.Frame.DisposeOperation.NONE:
                          break;
                      case APNGLib.Frame.DisposeOperation.BACKGROUND:
                          g.Clear(Color.Transparent);
                          break;
                      case APNGLib.Frame.DisposeOperation.PREVIOUS:
                          if (box.prevBuffer != null)
                          {
                              g.DrawImage(box.prevBuffer, Point.Empty);
                          }
                          else
                          {
                              g.Clear(Color.Transparent);
                          }
                          break;
                      default:
                          break;
                  }
                  Bitmap read = png.ToBitmap(box.frameNum++);
                  switch (f.BlendOp)
                  {
                      case APNGLib.Frame.BlendOperation.OVER:
                          g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
                          break;
                      case APNGLib.Frame.BlendOperation.SOURCE:
                          g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                          break;
                      default:
                          break;
                  }
                  g.DrawImage(read, new Point((int)f.XOffset, (int)f.YOffset));
              }
  
              box.prevBuffer = box.buffer;
              pb.Image = box.buffer;
  
              if (box.frameNum &gt;= box.png.FrameCount)
              {
                  box.frameNum = 0;
                  box.buffer = null;
                  box.prevBuffer = null;
              }
          }
      }
  </code></pre>
  
  <p>The <code>APNGBox</code> is</p>
  
  <pre><code>internal class APNGBox
      {
          public int frameNum = 0;
          public APNGLib.APNG png;
          public Bitmap buffer;
          public Bitmap prevBuffer;
      }
  </code></pre>
  
  <p>I think that this is mostly correct, as I've run it against all the images in the <a href="http://s826.photobucket.com/albums/zz190/apng/" rel="noreferrer">APNG Gallery</a>. However, some of them are rendering incorrectly (<a href="http://s826.photobucket.com/albums/zz190/apng/?action=view&amp;current=graham11.png" rel="noreferrer">This</a> one has artifacting issues, and <a href="http://s826.photobucket.com/albums/zz190/apng/?action=view&amp;current=APNG-from-GIF-Mouse.png" rel="noreferrer">this</a> one does not retain the red bar on the left consistently). Please note, you'll have the view the page in Firefox 3 or higher to view the animations.</p>
  
  <p>I'm lead to believe the issue has to do with how I handle the DISPOSE_OP_PREVIOUS, but I can't figure out what I'm doing wrong. Can anyone suggest what I might be missing?</p>