码迷,mamicode.com
首页 > Windows程序 > 详细

WPF重写Image实现动态图片--未测试

时间:2016-07-23 16:39:56      阅读:778      评论:0      收藏:0      [点我收藏+]

标签:

WPF很强大,但是当WPF的image控件遇到gif时就只读了图片的第一帧,很好很强大!

WPF不屑于gif的简单动画!
幸好WPF里有MediaElement这个东西,它是对MediaPlyer的一个封装,果然很强大啊。不过另我不爽的是我这里有N个gif图片就要有N个MediaElement,要了亲命了。

还是不好,如果你能想到用WebBrowseControl来实现,或者用Frame来实现,恭喜你,你太有才了!
重写一下WPF的image,good idea!

    public class GIFImageControl : System.Windows.Controls.Image
    {
        delegate void OnFrameChangedDelegate();
        private Bitmap m_Bitmap;
        public string Path { get; set; }
        BitmapSource bitmapSource;

        public void AnimatedImageControl(string path)
        {
            Path = path;
            m_Bitmap = (Bitmap)Image.FromFile(path);
            Width = m_Bitmap.Width;
            Height = m_Bitmap.Height;
            ImageAnimator.Animate(m_Bitmap, OnFrameChanged);
            bitmapSource = GetBitmapSource();
            Source = bitmapSource;

        }


        private void OnFrameChanged(object sender, EventArgs e)
        {

            Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                   new OnFrameChangedDelegate(OnFrameChangedInMainThread));

        }

        private void OnFrameChangedInMainThread()
        {
            ImageAnimator.UpdateFrames();
            if (bitmapSource != null)
                bitmapSource.Freeze();
            bitmapSource = GetBitmapSource();
            Source = bitmapSource;
            InvalidateVisual();
        }

        //private static bool loaded;
        private BitmapSource GetBitmapSource()
        {
            IntPtr inptr = m_Bitmap.GetHbitmap();
            bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
                inptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            DeleteObject(inptr);
            return bitmapSource;
        }

        [DllImport("gdi32")]
        static extern int DeleteObject(IntPtr o);
    }

 

转自:http://www.iteye.com/topic/1115983

WPF重写Image实现动态图片--未测试

标签:

原文地址:http://www.cnblogs.com/SevenKill/p/5698864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!