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?”

9 thoughts on “C#, WPF – Play swf in wpf application”

  1. what is SWFFileHeader swfFile = new SWFFileHeader(strFilePath);

    got error that said no namespace..
    please help. urgent!!!

    thanks~

  2. I can play SWF in WPF but can’t stop the SWF sound with a disable SWF button. Appreciate to help to resolve it, thanks!

    Best Regards,
    Robert Yuan

  3. hi Robert Yuan,
    i have some problem while playinf flash file in WPF.can u plz tell me how you have done that????when i try to add shockwave flash object in my WPF application,it does not show any flash object in toolbar.can you plz help me
    thanks in advance
    you can contact me on sun007il@gmail.com

  4. Hi,

    I use visual studio 2010, and I can’t find ‘com component’ in the toolbox!! How can I play a flash file in a windows application form?
    I’ll be very thanksful if help me.

    thanks in advance

  5. I tried as you said but shockwave flash object not getting added to my toolbar.. i also tried Interop.ShockwaveFlashObjects and AxInterop.ShockwaveFlashObjects. but this is also not working.
    please help me.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.