WPF 中的 GIF 动画
在 WPF 中动画化透明 GIF 图像。
引言
我找到了一个仅使用 Windows Forms 实现 GIF 动画的解决方案。基于此,我在 WPF (.NET 3.5) 中解决了这个问题。
Using the Code
我创建了自己的类
public class AnimatedImageControl : UserControl {...}
并重写了 OnRender
函数
protected override void OnRender(DrawingContext p_drawingContext)
{
//Get the next frame ready for rendering.
ImageAnimator.UpdateFrames(m_animatedImage);
//Draw the next frame in the animation.
Graphics gr = Graphics.FromHwnd(m_hwnd.Handle);
gr.FillRectangle(m_brush, m_rectangle);
gr.DrawImage(m_animatedImage, m_rectangle);
}
关注点
主要技巧是使用 Graphics
而不是 DrawingContext
。
只有当背景是纯色画笔时,它才能正常工作,因为需要调用 FillRectangle()
。