private static Dictionary <int, Animator.GifPalette> CreatePalettes(GifDataStream metadata) { Dictionary <int, Animator.GifPalette> dictionary = new Dictionary <int, Animator.GifPalette>(); Color[] colorArray = (Color[])null; if (metadata.Header.LogicalScreenDescriptor.HasGlobalColorTable) { colorArray = (Color[])Enumerable.ToArray <Color>(Enumerable.Select <GifColor, Color>(metadata.GlobalColorTable, (Func <GifColor, Color>)(gc => Color.FromArgb(byte.MaxValue, gc.R, gc.G, gc.B)))); } for (int index = 0; index < metadata.Frames.Count; ++index) { GifFrame frame = metadata.Frames[index]; Color[] colors = colorArray; if (frame.Descriptor.HasLocalColorTable) { colors = (Color[])Enumerable.ToArray <Color>(Enumerable.Select <GifColor, Color>(frame.LocalColorTable, (Func <GifColor, Color>)(gc => Color.FromArgb(byte.MaxValue, gc.R, gc.G, gc.B)))); } int?transparencyIndex = new int?(); GifGraphicControlExtension graphicControl = frame.GraphicControl; if (graphicControl != null && graphicControl.HasTransparency) { transparencyIndex = new int?(graphicControl.TransparencyIndex); } dictionary[index] = new Animator.GifPalette(transparencyIndex, colors); } return(dictionary); }
private async Task RenderFrameAsync(int frameIndex, CancellationToken cancellationToken) { if (frameIndex < 0) { return; } GifFrame frame = this._metadata.Frames[frameIndex]; GifImageDescriptor desc = frame.Descriptor; using (Stream indexStreamAsync = await this.GetIndexStreamAsync(frame, cancellationToken)) { if (frameIndex < this._previousFrameIndex) { this.ClearArea((IGifRect)this._metadata.Header.LogicalScreenDescriptor); } else { this.DisposePreviousFrame(frame); } int length = 4 * desc.Width; byte[] buffer = new byte[desc.Width]; byte[] numArray = new byte[length]; Animator.GifPalette palette = this._palettes[frameIndex]; int num1 = palette.TransparencyIndex ?? -1; foreach (int num2 in frame.Descriptor.Interlace ? Animator.InterlacedRows(frame.Descriptor.Height) : Animator.NormalRows(frame.Descriptor.Height)) { if (indexStreamAsync.Read(buffer, 0, desc.Width) != desc.Width) { throw new EndOfStreamException(); } int offset = (desc.Top + num2) * this._stride + desc.Left * 4; if (num1 >= 0) { Animator.CopyFromBitmap(numArray, this._bitmap, offset, length); } for (int index = 0; index < desc.Width; ++index) { byte num3 = buffer[index]; int startIndex = 4 * index; if ((int)num3 != num1) { Animator.WriteColor(numArray, palette[(int)num3], startIndex); } } Animator.CopyToBitmap(numArray, this._bitmap, offset, length); } this._bitmap.Invalidate(); this._previousFrame = frame; this._previousFrameIndex = frameIndex; } }