如何将Gif图像添加到wpf

时间:2012-05-26 07:53:54

标签: c# wpf

现在我正在使用WPF和C#开发一个项目,其中一些部分与动画图像有关。 那么每个人都可以告诉我如何在表单中添加.gif文件吗? 感谢。

3 个答案:

答案 0 :(得分:1)

这是一个完整的代码,您必须在主要的namesapce中包含以下类。 你的主要wpf表单就像这样称呼它,

    <UI:AnimatedGIFControl x:Name="GIFCtrl" Visibility="Collapsed" VerticalAlignment="Center" Margin="200,0,0,0" Width="380" Height="48"/>

其中UI是定义AnimatedGIFControl的namesapce。您可以像这样调用它

          xmlns:UI="clr-namespace:xyz"

完整的类代码是,

           class AnimatedGIFControl : System.Windows.Controls.Image
{
    private Bitmap _bitmap; // Local bitmap member to cache image resource
    private BitmapSource _bitmapSource;
    public delegate void FrameUpdatedEventHandler();


    /// <summary>
    /// Delete local bitmap resource
    /// Reference: http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx
    /// </summary>
    [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern bool DeleteObject(IntPtr hObject);

    /// <summary>
    /// Override the OnInitialized method
    /// </summary>
    protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);
        this.Loaded += new RoutedEventHandler(AnimatedGIFControl_Loaded);
        this.Unloaded += new RoutedEventHandler(AnimatedGIFControl_Unloaded);
    }

    /// <summary>
    /// Load the embedded image for the Image.Source
    /// </summary>

    void AnimatedGIFControl_Loaded(object sender, RoutedEventArgs e)
    {
       // Get GIF image from Resources


        if (Properties.Resources.ProgressIndicator != null)
        {
            _bitmap = Properties.Resources.ProgressIndicator;
            Width = _bitmap.Width;
            Height = _bitmap.Height;

            _bitmapSource = GetBitmapSource();
            Source = _bitmapSource;
        }
    }

    /// <summary>
    /// Close the FileStream to unlock the GIF file
    /// </summary>
    private void AnimatedGIFControl_Unloaded(object sender, RoutedEventArgs e)
    {
        StopAnimate();
    }

    /// <summary>
    /// Start animation
    /// </summary>
    public void StartAnimate()
    {
        ImageAnimator.Animate(_bitmap, OnFrameChanged);
    }

    /// <summary>
    /// Stop animation
    /// </summary>
    public void StopAnimate()
    {
        ImageAnimator.StopAnimate(_bitmap, OnFrameChanged);
    }

    /// <summary>
    /// Event handler for the frame changed
    /// </summary>
    private void OnFrameChanged(object sender, EventArgs e)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                               new FrameUpdatedEventHandler(FrameUpdatedCallback));
    }

    private void FrameUpdatedCallback()
    {
        ImageAnimator.UpdateFrames();

        if (_bitmapSource != null)
            _bitmapSource.Freeze();

        // Convert the bitmap to BitmapSource that can be display in WPF Visual Tree
        _bitmapSource = GetBitmapSource();
        Source = _bitmapSource;
        InvalidateVisual();
    }

    private BitmapSource GetBitmapSource()
    {
        IntPtr handle = IntPtr.Zero;

        try
        {
            handle = _bitmap.GetHbitmap();
            _bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
                handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }
        finally
        {
            if (handle != IntPtr.Zero)
                DeleteObject(handle);
        }

        return _bitmapSource;
    }

}

并像这样使用

                   GIFCtrl.StartAnimate();
希望它可以帮助你。

答案 1 :(得分:0)

不幸的是,在WPF中,Gif动画不可见。即使您在图像控件中加载一个,您也只能看到它的第一帧。

答案 2 :(得分:0)

Image不支持此功能,但使用自定义控件或包装WinForms控件的duplicate answer上的某些解决方案似乎是很好的解决方案。