On developing application forms it may sometimes be required to display animated GIF image. The ImageBox component enables the user to display only static images, the Drawing assembly does not contain resources required to work with animated images. To solve the problem, it is available to implement a custom component in the Fore.NET language that inherits the System.Windows.Forms.PictureBox component, and use the System.Drawing.ImageAnimator class for animation. Such a component on the Fore form can be displayed in the NetControlBox component.
The example of custom Fore.NET component:
Imports System.Drawing;
Imports System.Windows.Forms;
Public Class AnimatedBox: PictureBox
animatedImage: Bitmap;
currentlyAnimating: Boolean = False;
Public Constructor Create(Path: string);
Begin
animatedImage := New Bitmap(Path);
Self.Width := 100;
Self.Height := 100;
Self.Paint += New PaintEventHandler(OnPaint);
End Constructor;
Private Sub OnFrameChanged(o: object; e: System.EventArgs);
Begin
//Forced component rendering
Self.Invalidate();
End Sub OnFrameChanged;
Private Sub OnPaint(sender: System.Object; e: System.Windows.Forms.PaintEventArgs);
Begin
//On the first component rendering displaying of animated image is initialized
If (Not currentlyAnimating) Then
ImageAnimator.Animate(animatedImage, New System.EventHandler(OnFrameChanged));
currentlyAnimating := True;
End If;
//Refresh frame
ImageAnimator.UpdateFrames();
//Render new frame
e.Graphics.DrawImage(animatedImage, New Point(0, 0));
End Sub;
End Class;
The path to the animated GIF image is passed as a parameter for component constructor. After the component is subscribed for the Paint event, on the first rendering the image is animated.
To connect this component on the Fore form, use the following code:
Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Var
MB: IMetabase;
Run: IForeNETRuntime;
NETAsm: IForeNETAssembly;
Asm, Asm1: IForeNETRuntimeAssembly;
Arg: IForeNETRuntimeMethodArgs;
Bind: IForeNETRuntimeMethodBinding;
Typ, Typ1: IForeNETRuntimeType;
Obj: IForeNETRuntimeObjectInstance;
Begin
MB := MetabaseClass.Active;
Run := ForeNETAssemblyClass.Runtime;
NETAsm := MB.ItemById("UserComponents").Bind As IForeNETAssembly;
Asm := Run.Assembly(NETAsm);
Asm1 := Run.SystemAssembly("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac");
Typ := Asm.Type("UserComponents.AnimatedBox");
Typ1 := Asm1.Type("System.String");
Arg := Run.CreateArgs(1);
//Path to the file is passed to constructor as a parameter
Arg.Value(0) := "C:\sample.gif";
Bind := Run.CreateBinding(1);
Bind.Types.Item(0) := Typ1;
//Create a component instance
Obj := Typ.CreateInstance(Arg, Bind);
//Display component in NetControlBox
NetControlBox1.AttachControl(Obj);
End Sub Button1OnClick;
See also: