private void createImage(uint[] colorIndexes, ImageDescriptor imageDescriptor, ColorTable localColorTable) { uint localX = imageDescriptor.X; uint localY = imageDescriptor.Y; uint localW = imageDescriptor.Width; uint localH = imageDescriptor.Height; //Every pixel consists of 4 bytes: R, G, B, A values byte[] data = new byte[localW * localH * 4]; int transparentColorIndex = -1; int delay = 0; FrameDisposalMethod disposalMethod = FrameDisposalMethod.Clear; if (_extensions.Count > 0 && _extensions.Last() is GraphicsControlExtension) { GraphicsControlExtension gce = _extensions.Last() as GraphicsControlExtension; delay = gce.Delay; if (gce.HasTransparency) { transparentColorIndex = gce.TransparentColorIndex; } disposalMethod = gce.DisposalMethod; } for (int i = 0; i < data.Length; i += 4) { Color color = localColorTable[(int)colorIndexes[i / 4]]; data[i + 0] = color.R; data[i + 1] = color.G; data[i + 2] = color.B; if (colorIndexes[i / 4] == transparentColorIndex) { data[i + 3] = 0; } else { data[i + 3] = byte.MaxValue; } } if (imageDescriptor.Interlaced) { data = interlace(data, localW, localH); } _images.Add(new GifImage(data, localX, localY, localW, localH, delay, disposalMethod)); }
private static ObjectAnimationUsingKeyFrames GetAnimation(Image imageControl, BitmapSource source) { ObjectAnimationUsingKeyFrames animation = AnimationCache.GetAnimation(source, GetRepeatBehavior(imageControl)); if (animation == null) { GifFile file; GifBitmapDecoder decoder = GetDecoder(source, out file) as GifBitmapDecoder; if ((decoder == null) || (decoder.Frames.Count <= 1)) { return(null); } Int32Size fullSize = GetFullSize(decoder, file); int frameIndex = 0; animation = new ObjectAnimationUsingKeyFrames(); TimeSpan zero = TimeSpan.Zero; BitmapSource baseFrame = null; foreach (BitmapFrame frame in decoder.Frames) { FrameMetadata metadata = GetFrameMetadata(decoder, file, frameIndex); BitmapSource source3 = MakeFrame(fullSize, frame, metadata, baseFrame); DiscreteObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(source3, zero); animation.KeyFrames.Add(keyFrame); zero += metadata.Delay; FrameDisposalMethod disposalMethod = metadata.DisposalMethod; switch (disposalMethod) { case FrameDisposalMethod.None: case FrameDisposalMethod.DoNotDispose: baseFrame = source3; break; case FrameDisposalMethod.RestoreBackground: baseFrame = !IsFullFrame(metadata, fullSize) ? ClearArea(source3, metadata) : null; break; default: break; } frameIndex++; } animation.Duration = zero; animation.RepeatBehavior = GetActualRepeatBehavior(imageControl, decoder, file); AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation); AnimationCache.IncrementReferenceCount(source, GetRepeatBehavior(imageControl)); } return(animation); }
public GifImage(byte[] data, uint x, uint y, uint width, uint height, int delay, FrameDisposalMethod disposalMethod) { Data = data; Width = width; Height = height; X = x; Y = y; Delay = (uint)delay; }