コード例 #1
0
        private void UpdateAnimatedBitmap()
        {
            this.StopAnimate();

            if (this.animatedBitmapStream != null)
            {
                this.animatedBitmapStream.Close();
                this.animatedBitmapStream = null;
                this.frames             = null;
                this.currentFrameNumber = 0;
            }

            if (this.AnimatedBitmap != null)
            {
                Stream ImageStream;
                try
                {
                    if (this.AnimatedBitmap.IsAbsoluteUri && File.Exists(this.AnimatedBitmap.AbsoluteUri))
                    {
                        ImageStream = File.OpenRead(this.AnimatedBitmap.AbsoluteUri);
                    }
                    else
                    {
                        StreamResourceInfo Resource = Application.GetResourceStream(this.AnimatedBitmap);
                        if (Resource == null)
                        {
                            throw new InvalidOperationException($"Resource '{this.AnimatedBitmap.OriginalString}' not found!");
                        }
                        else
                        {
                            ImageStream = Resource.Stream;
                        }
                    }
                }
                catch (Exception E)
                {
                    throw new InvalidOperationException($"Image could not be loaded: '{this.AnimatedBitmap.OriginalString}'", E);
                }

                this.animatedBitmapStream = ImageStream;
                this.animatedBitmap       = new System.Drawing.Bitmap(this.animatedBitmapStream);

                int FrameCount = this.animatedBitmap.GetFrameCount(FrameDimension.Time);
                this.frames = new ImageSource[FrameCount];


                for (int FrameNumber = 0; FrameNumber < FrameCount; FrameNumber++)
                {
                    this.animatedBitmap.SelectActiveFrame(FrameDimension.Time, FrameNumber);
                    this.animatedBitmap.MakeTransparent();
                    IntPtr BitmapHandle = this.animatedBitmap.GetHbitmap();
                    this.frames[FrameNumber] = Imaging.CreateBitmapSourceFromHBitmap(
                        BitmapHandle,
                        IntPtr.Zero,
                        Int32Rect.Empty,
                        BitmapSizeOptions.FromEmptyOptions());
                    AnimatedImage.DeleteObject(BitmapHandle);
                }
            }

            this.StartAnimate();
        }
コード例 #2
0
        private static void OnAnimatedBitmapChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            AnimatedImage Control = (AnimatedImage)obj;

            Control.UpdateAnimatedBitmap();
        }