leaksmei
leaksmei

Reputation: 11

How to add Gif image to wpf

Now I am developing on one project with WPF and C#, some parts it related to animation image. So can everyone tell me how to add .gif file the the form ? Thanks.

Upvotes: 0

Views: 6414

Answers (3)

Waseem
Waseem

Reputation: 1

This is a complete code,you have to just include below class in to your main namesapce. and your main wpf form call it like this,

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

where UI is the namesapce where AnimatedGIFControl is defined.you can call it like this

          xmlns:UI="clr-namespace:xyz"

The Complete class code is ,

           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;
    }

}

and use like this

                   GIFCtrl.StartAnimate();

hope it may help you.

Upvotes: 0

K893824
K893824

Reputation: 1319

This isn't supported by Image, but some of the solutions on this duplicate answer using a custom control or wrapping a WinForms control seem to be good solutions.

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

Unfortunately in WPF Gif animation is not visible. Even if you load one in a image control, you will only see its first frame.

Upvotes: 0

Related Questions