Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How I can draw an aniamted gif on WPF I tried this following code but that draw the image without any animation. Is it possible to draw a gif animation in WPF?

C#
private void DrawPanelSurface(Model3DGroup model_group)
       {   // Make a mesh to hold the surface.
           MeshGeometry3D mesh = new MeshGeometry3D();

           // Triangle 1.
           // Set the triangle's points.
           mesh.Positions.Add(new Point3D(-10, -10, -10));
           mesh.Positions.Add(new Point3D(-10, -10, 10));
           mesh.Positions.Add(new Point3D(10, -10, 10));

           // Set the points' texture coordinates.
           mesh.TextureCoordinates.Add(new System.Windows.Point(0, 0));
           mesh.TextureCoordinates.Add(new System.Windows.Point(0, 1));
           mesh.TextureCoordinates.Add(new System.Windows.Point(1, 1));

           // Create the triangle.
           mesh.TriangleIndices.Add(0);
           mesh.TriangleIndices.Add(1);
           mesh.TriangleIndices.Add(2);

           // Triangle 2.
           // Set the triangle's points.
           mesh.Positions.Add(new Point3D(-10, -10, -10));
           mesh.Positions.Add(new Point3D(10, -10, 10));
           mesh.Positions.Add(new Point3D(10, -10, -10));

           // Set the points' texture coordinates.
           mesh.TextureCoordinates.Add(new System.Windows.Point(0, 0));
           mesh.TextureCoordinates.Add(new System.Windows.Point(1, 1));
           mesh.TextureCoordinates.Add(new System.Windows.Point(1, 0));

           // Create the triangle.
           mesh.TriangleIndices.Add(3);
           mesh.TriangleIndices.Add(4);
           mesh.TriangleIndices.Add(5);

           // Make the surface's material using an image brush.
           ImageBrush imagebrush = new ImageBrush();
           imagebrush.ImageSource =
               new BitmapImage(new Uri("giphy.gif", UriKind.Relative));
           DiffuseMaterial image_material = new DiffuseMaterial(imagebrush);

           GeometryModel3D surface_model = new GeometryModel3D(mesh, image_material);

           // Make the surface visible from both sides.
           surface_model.BackMaterial = image_material;

           // Add the model to the model groups.
           model_group.Children.Add(surface_model);
       }
Posted

The problem is not related to the position of you GIF in the logical tree, ViewPort3D or anything else. This is the problem by itself. You won't directly see GIF animation in WPF.

To solve this problem, please see, for example, this open-source product: https://github.com/thomaslevesque/WpfAnimatedGif.

See also these CodeProject articles on the topic:
GIF Animation in WPF,
Another approach to animated GIF in WPF, with transparency.

Good luck,
—SA
 
Share this answer
 
Comments
Arun Kumar K S 22-Apr-15 0:38am    
I didnt find any code to draw gif in ViewPort3D. All the above axamples are in WPF but all are explain to draw in 2D.
Sergey Alexandrovich Kryukov 22-Apr-15 2:20am    
Oh... this is a whole different story. All right, please see Solution 2.
—SA
Arun Kumar asked:

I didn't find any code to draw gif in ViewPort3D. All the above axamples are in WPF but all are explain to draw in 2D.
I did not know that you did not know that; the question stated the animation problem. This is a whole different story.

Actually, you need a material to render anything 2D on 3D. You can use the class Viewport2DVisual3D:
https://msdn.microsoft.com/en-us/library/system.windows.media.media3d.viewport2dvisual3d%28v=vs.110%29.aspx[^].

After you define all Viewport2DVisual3D properties, you can add you image as the last child. In the article referenced below, there is a code sample demonstrating XAML with 2D Button shown as a child of a Viewport2DVisual3D instance:
WPF 3D: Part 1 of n[^].

Don't find code, find documentation and perhaps available answers.

—SA
 
Share this answer
 
Comments
Arun Kumar K S 23-Apr-15 0:26am    
Thank you for this answer.
Sergey Alexandrovich Kryukov 23-Apr-15 0:47am    
You are welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900