การสร้างภาพเคลื่อนไหว Gif ใน WPF

ฉันใช้รหัสนี้สำหรับ gif Animating ในไลบรารีแยกและรหัส xaml ในโครงการหลักของฉัน:

<controls:GifImage GifSource="/project;component/Images/my.gif" Stretch="None" />

Gif Animating (ไฟล์แยก):

public class GifImage : Image
    {
        #region Memmbers

        private GifBitmapDecoder _gifDecoder;
        private Int32Animation _animation;
        private bool _isInitialized;

        #endregion Memmbers

        #region Properties

        private int FrameIndex
        {
            get { return (int)GetValue(FrameIndexProperty); }
            set { SetValue(FrameIndexProperty, value); }
        }

        private static readonly DependencyProperty FrameIndexProperty =
         DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImage), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex)));

        private static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
        {
            GifImage image = obj as GifImage;
            image.Source = image._gifDecoder.Frames[(int)ev.NewValue];
        }

        /// <summary>
        /// Defines whether the animation starts on it's own
        /// </summary>
        public bool AutoStart
        {
            get { return (bool)GetValue(AutoStartProperty); }
            set { SetValue(AutoStartProperty, value); }
        }

        public static readonly DependencyProperty AutoStartProperty =
         DependencyProperty.Register("AutoStart", typeof(bool), typeof(GifImage), new UIPropertyMetadata(false, AutoStartPropertyChanged));

        private static void AutoStartPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
                (sender as GifImage).StartAnimation();
        }

        public string GifSource
        {
            get { return (string)GetValue(GifSourceProperty); }
            set { SetValue(GifSourceProperty, value); }
        }

        public static readonly DependencyProperty GifSourceProperty =
         DependencyProperty.Register("GifSource", typeof(string), typeof(GifImage), new UIPropertyMetadata(string.Empty, GifSourcePropertyChanged));

        private static void GifSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            // CARLO 20100622: Reinitialize animation everytime image is changed
            (sender as GifImage).Initialize();
        }

        #endregion Properties

        #region Private Instance Methods

        private void Initialize()
        {

            _gifDecoder = new GifBitmapDecoder(new Uri(String.Format("pack://application:,,,{0}", this.GifSource)), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            _animation = new Int32Animation(0, _gifDecoder.Frames.Count - 1, new Duration(new TimeSpan(0, 0, 0, _gifDecoder.Frames.Count / 10, (int)((_gifDecoder.Frames.Count / 10.0 - _gifDecoder.Frames.Count / 10) * 1000))));
            _animation.RepeatBehavior = RepeatBehavior.Forever;
            this.Source = _gifDecoder.Frames[0];

            _isInitialized = true;
        }

        #endregion Private Instance Methods

        #region Public Instance Methods

        /// <summary>
        /// Shows and starts the gif animation
        /// </summary>
        public void Show()
        {
            this.Visibility = Visibility.Visible;
            this.StartAnimation();
        }

        /// <summary>
        /// Hides and stops the gif animation
        /// </summary>
        public void Hide()
        {
            this.Visibility = Visibility.Collapsed;
            this.StopAnimation();
        }

        /// <summary>
        /// Starts the animation
        /// </summary>
        public void StartAnimation()
        {
            if (!_isInitialized)
                this.Initialize();

            BeginAnimation(FrameIndexProperty, _animation);
        }

        /// <summary>
        /// Stops the animation
        /// </summary>
        public void StopAnimation()
        {
            BeginAnimation(FrameIndexProperty, null);
        }

        #endregion Public Instance Methods
    }

แต่ฉันได้รับข้อผิดพลาด:

ไม่รู้จักคำนำหน้า URI

ฉันไม่แน่ใจว่าทำไมฉันถึงได้รับข้อผิดพลาด ใครสามารถช่วยฉันหน่อยได้ไหม?


person UFO    schedule 30.10.2013    source แหล่งที่มา
comment
ดูเหมือนว่าโค้ดจะมาจากคำตอบนี้ มีคำตอบที่ได้รับการโหวตสูงกว่าในหน้าเดียวกันซึ่งใช้แพ็คเกจ NuGet ชื่อ XamlAnimatedGif (nee WpfAnimatedGif) รวมถึงโซลูชันอื่น ๆ อีกมากมาย   -  person fadden    schedule 14.12.2019


คำตอบ (3)


มีวิธีที่ง่ายกว่ามากในการแสดง .gif แบบเคลื่อนไหวใน wpf - ใช้ MediaElement

ตัวอย่าง:

<MediaElement x:Name="myGif" MediaEnded="myGif_MediaEnded" UnloadedBehavior="Manual"     Source="file://C:\waiting.GIF" LoadedBehavior="Play" Stretch="None"/>

หากคุณต้องการให้ .gif วนซ้ำอย่างไม่มีที่สิ้นสุด แต่ระบุเฉพาะจำนวนการทำซ้ำในไฟล์ .gif ที่จำกัด คุณสามารถเชื่อมต่อ MediaEnded และรีสตาร์ทแอนิเมชั่นได้ (อย่าลืมตั้งค่า UnloadedBehavior เป็น Manual):

    private void myGif_MediaEnded(object sender, RoutedEventArgs e)
    {
        myGif.Position = new TimeSpan(0, 0, 1);
        myGif.Play();
    }
person edtheprogrammerguy    schedule 30.10.2013
comment
ฉันจะระบุตำแหน่งที่ GIF เล่นบนหน้าจอได้อย่างไร - person zetar; 13.07.2016
comment
@Wobbles - ไม่ใช่เหตุผลที่จะลงคะแนนเสียง ขอบคุณสำหรับความเข้าใจ - person edtheprogrammerguy; 18.07.2017
comment
@edtheprogrammerguy Answer มีแอปพลิเคชันที่จำกัด และจะทำให้เกิดปัญหากับผู้ใช้จำนวนมาก ดังนั้นจึงไม่เป็นที่พอใจว่าทำไมผู้ใช้จำนวนมากมาที่นี่เพื่อดูภาพเคลื่อนไหว gif - person Wobbles; 18.07.2017

ลองสิ่งนี้:

<controls:GifImage GifSource="/Images/my.gif" Stretch="None" />

อย่างไรก็ตาม ฉันพบว่าการใช้วิธีนี้ในการเล่น gif ใน wpf อาจทำให้รูปภาพ GIF บางภาพผิดรูป และฉันสงสัยว่าทำไม...

person Chobits    schedule 12.11.2013
comment
คุณพบการควบคุมนั้นที่ไหน? - person Magnus Ahlin; 24.06.2015

ฉันไม่สามารถรับเครดิตสำหรับสิ่งนี้ได้ แต่นี่คือวิธีดำเนินการใน XAML เท่านั้น ฉันเพิ่มคุณสมบัติ "IsBusy" ให้กับ ViewModel ของฉันเพื่อแสดง/ซ่อนสปินเนอร์ระหว่างการประมวลผล

        <Image Name="Spinner" Source="Resources/spinner.gif" RenderTransformOrigin="0.5, 0.5">
            <Image.Triggers>
                <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard Storyboard.TargetName="Spinner" Storyboard.TargetProperty="RenderTransform.(RotateTransform.Angle)">
                                <DoubleAnimation From="0" To="360" BeginTime="0:0:0" Duration="0:0:2" RepeatBehavior="Forever" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Image.Triggers>
            <Image.RenderTransform>
                <RotateTransform Angle="0" />
            </Image.RenderTransform>
            <Image.Style>
                <Style TargetType="Image">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsBusy}" Value="False">
                            <Setter Property="Visibility" Value="Hidden"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>

นี่คือลิงก์ ไปยังโซลูชันของผู้เขียน

person LawMan    schedule 16.08.2017
comment
นี่เป็นเพียงการหมุนภาพ (ภาพออกภาพแรกหากแหล่งที่มาเป็น .gif) - person Mohammad Mahroz; 19.11.2017
comment
หน้าที่เชื่อมโยงเกี่ยวข้องกับการแทนที่ภาพเคลื่อนไหว GIF สปินเนอร์แบบเคลื่อนไหวด้วยโซลูชันทางเลือก มีการให้คำตอบไว้สองวิธี โดยวิธีแรกจะแสดงอยู่ในคำตอบ (และไม่เกี่ยวข้องกับคำถาม) อีกอันใช้ WPF กระดานเรื่องราว เพื่อแสดงเฟรมตามลำดับ มีการสำรวจเพิ่มเติมอีกเล็กน้อยในคำตอบนี้ - person fadden; 14.12.2019