C#, WPF – Play swf in wpf application

When I was trying to extract the first frame of SWF file into image I discovered that there is a small problem when we try to host a SWF file in WPF application because WPF does not allow us to integrate ActiveX control directly in its application. To enable interaction through ActiveX control, we must do a walkthrough with WindowsFormsHost. In this small example below I would like to guide you step by step to host a SWF file in your WPF application.

1. In Visual Studio, File –> New –> Project –> Windows Forms Control

2. Set Platform target to x86

3. In Visual Studio, right click on Toolbox window, then “Choose Items”

4. Navigate to COM components tab and choose Shockwave Flash Object. Press OK.

5. Drag “Schockwave Flash Object” from Toolbox to our UserControl and change its name to axShockwaveFlash. Rename our UserControl to FlashAxControl and add following code to it.

public partial class FlashAxControl : UserControl
{
	public FlashAxControl()
	{
		InitializeComponent();
	}

	public new int Width
	{
		get { return axShockwaveFlash.Width; }
		set { axShockwaveFlash.Width = value; }
	}

	public new int Height
	{
		get { return axShockwaveFlash.Height; }
		set { axShockwaveFlash.Height = value; }
	}

	public void LoadMovie(string strPath)
	{
		axShockwaveFlash.LoadMovie(0, strPath);
	}

	public void Play()
	{
		axShockwaveFlash.Play();
	}

	public void Stop()
	{
		axShockwaveFlash.Stop();
	}
}

6. Build our UserControl. Start new WPF Application and reference to our UserControl, System.Windows.Forms and WindowsFormsIntegration as following

7. Set Platform Target to x86

8. Add following code to play SWF in our application

string strFilePath = @"e:\temp.swf";
SWFFileHeader swfFile = new SWFFileHeader(strFilePath);
this.Width = swfFile.FrameSize.WidthInPixels;
this.Height = swfFile.FrameSize.HeightInPixels;

WindowsFormsHost host = new WindowsFormsHost();
FormFlashLibrary.FlashAxControl player = new FormFlashLibrary.FlashAxControl();

//the Windows Forms Host hosts the Flash Player
host.Child = player;

//the WPF Grid hosts the Windows Forms Host
grdMain.Children.Add(host);

//set size
player.Width = (int) this.Width;
player.Height = (int) this.Height;

//load & play the movie
player.LoadMovie(strFilePath);
player.Play();

The grdMain is the grid of WPF Application. The complete source code you can download at “Form Flash Library” and “WPF Play SWF”.
If you want to play SWF in Windows Form Application, you can read at this blog “C# – How to play a .swf file?”