获取 GIF 在 WPF 中播放(使用 GifImage 类)
以下是我在这里找到的一段代码: 如何获取可使用的动画 gif WPF?
class GifImage : Image {
public int FrameIndex {
get { return (int)GetValue(FrameIndexProperty); }
set { SetValue(FrameIndexProperty, value); }
}
public static readonly DependencyProperty FrameIndexProperty =
DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImage), new UIPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex)));
static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev) {
GifImage ob = obj as GifImage;
ob.Source = ob.gf.Frames[(int)ev.NewValue];
ob.InvalidateVisual();
}
GifBitmapDecoder gf;
Int32Animation anim ;
public GifImage(Uri uri) {
gf = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
anim = new Int32Animation(0, gf.Frames.Count - 1, new Duration(new TimeSpan(0,0, 0,gf.Frames.Count/10,(int)((gf.Frames.Count/10.0-gf.Frames.Count/10)*1000))));
anim.RepeatBehavior = RepeatBehavior.Forever;
Source = gf.Frames[0];
}
bool animationIsWorking = false;
protected override void OnRender(DrawingContext dc) {
base.OnRender(dc);
if (!animationIsWorking) {
BeginAnimation(FrameIndexProperty, anim);
animationIsWorking = true;
}
}
}
它应该允许我使用 GifImage 作为控件。但是
- 如何在 XAML 中使用它呢?
- 如何设置来源?我尝试了各种组合但不起作用并且没有文档:
GifImage gifImg = 新 GifImage(新 Uri(“/application;component/Images/indicator.gif”);
不起作用,异常告诉我一条错误的路径,但我就是找不到正确的路径。帮助..
Following is a piece of code I found here:
How do I get an animated gif to work in WPF?
class GifImage : Image {
public int FrameIndex {
get { return (int)GetValue(FrameIndexProperty); }
set { SetValue(FrameIndexProperty, value); }
}
public static readonly DependencyProperty FrameIndexProperty =
DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImage), new UIPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex)));
static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev) {
GifImage ob = obj as GifImage;
ob.Source = ob.gf.Frames[(int)ev.NewValue];
ob.InvalidateVisual();
}
GifBitmapDecoder gf;
Int32Animation anim ;
public GifImage(Uri uri) {
gf = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
anim = new Int32Animation(0, gf.Frames.Count - 1, new Duration(new TimeSpan(0,0, 0,gf.Frames.Count/10,(int)((gf.Frames.Count/10.0-gf.Frames.Count/10)*1000))));
anim.RepeatBehavior = RepeatBehavior.Forever;
Source = gf.Frames[0];
}
bool animationIsWorking = false;
protected override void OnRender(DrawingContext dc) {
base.OnRender(dc);
if (!animationIsWorking) {
BeginAnimation(FrameIndexProperty, anim);
animationIsWorking = true;
}
}
}
It supposed to allow me to use GifImage as a Control. But
- How do I use it in XAML?
- How do I set the Source? I tried various combination but doesn't work and there's no documentation:
GifImage gifImg = new GifImage(new
Uri("/application;component/Images/indicator.gif");
doesn't work and exception tells me a wrong path but I just can't get the right path. Help..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对上面的代码做了一些修改,似乎可以工作。
Xaml:
代码:
I modified the above code a little and it seems to work.
Xaml:
Code: