0

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

4

3 回答 3

1

这是一个完整的代码,您只需将以下类包含到您的主命名空间中。和你的主要 wpf 形式这样称呼它,

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

其中 UI 是定义 AnimatedGIFControl 的命名空间。你可以这样称呼它

          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();

希望它可以帮助你。

于 2012-05-31T06:49:27.217 回答
0

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

于 2012-05-26T07:57:11.373 回答
0

Image 不支持这一点,但是使用自定义控件或包装 WinForms 控件的这个重复答案的一些解决方案似乎是很好的解决方案。

于 2012-05-26T08:00:21.997 回答