C# – How to play a .swf file?

I am making a small research to convert a .swf file to an .exe file. I intend to build an .exe file with its resources which are .swf files. I will use this .exe file as a template later and just replace the resources with the .swf file which I want to convert. During the development I discover that I had a problem which I still did not solve. I could never load a .swf file in my application before. There are always problem when I tried to use the OCX control provided by Adobe.

Error loading ocx control
It happens always with Visual Studio 2005 at my computer although I registered it and re-registered it many times. I think there is a conflict between the version of .ocx control which I use and the version which VS 2005 expects. So today I tried with Visual Studio 2008 with the hope that it will work. So I do as following to insert a flash player to my application.

1. Right click on Toolbox window –> Choose Items…
Toolbox - Choose Items
2. Wait for a moment. On new window, click on Tab “COM Components” –> Shockwave Flash Object –> Click OK.

COM Component - Shockwave Flash Object

3. If it was successful, you would see the object Shockwave Flash Object on the Toolbox windows.

Shockwave Flash Object

4. Drag and Drop this component on Form.

Component on Form

5. Rename the control to chartFlashPlayer .Add this code to Form_Load event.

private void Form1_Load(object sender, EventArgs e)
{
chartFlashPlayer.LoadMovie(0, "D:\\fmared.swf");
}

6. Run form and I get it run. Ju hu.
Load successfully

I know this post may contain nothing helpful but anyways it’s first time that I can load .swf file in my application my computer at home. I feel happy. In next step I will try to load .swf file from resources and create a program to change the resource section so that I can replace with the file which I want. I hope everything will run smoothly. For more detailed instructions, you can follow this link http://www.adobe.com/devnet/flash/articles/stock_history.html

C# – How to read header of SWF file?

As I said in my previous blog about “How to play a swf file” yesterday, I am making a small tool to convert .swf file to an .exe file. In this tool I will wrap the .swf file in a dialog and play it. So it leads to a small problem that the width and height of dialog and ocx control containing .swf file should be adjusted to show the file correctly. To achieve this requirement I need some info about the width and height of swf which can be derived directly from .swf file. After some searching with Google for a while to look for a available source code helping me read info with c#, I decide to write own for myself because I realize it is not difficult to write my own and the other reason is the source code I found, does not work correctly.

First step, I need the SWF Specification Format which can be downloaded from adobe’s site. Just a moment going through the document I found what I need. That is the SWF Header.

SWF Header

All detailed information you can read more in the documentation of adobe. I just post here some important information which I really need during programming.

1. An FWS signature indicates an uncompressed SWF file; CWS indicates that the entire file after the first 8 bytes (that is, after the FileLength field) was compressed by using the ZLIB open standard. <–  I did not read carefully at this point. I think it will compress data after the header not inclusive the header and as consequence  I lost half hour to find out the problem.

2. In the SWF format, a twip is 1/20th of a logical pixel. A logical pixel is the same as a screen pixel when the file is played at 100%—that is, without scaling. <– Nothing special here, I just do not know what a twip is :).

3. 8.8 signed fixed-point numbers. The high 8 bits represent the number before the decimal point, and the low 8 bits represent the number after the decimal point <– Are you sure that you understand this statement? I am not. An example must be better and here is it. For example: The real value 7.5 is equivalent to: 0x07.0x80.This value is stored in the SWF file as: 80 07. Still not understand. Change them to binary and you’ll understand it.

I think I discussed all important information about SWF Header. And at last, a source code as result which is SWFFileHeader. It is a .cs file (not a .dll) so that you can use it anywhere you want, for example in Silverlight.

UPDATE 04/09/2010
To read the version of ActionScript which used in swf file we need to dig deeper into the SWF File structure. As shown in documentation of Adobe, the SWF File structure looks like below

SWF file structure

As you can see, after the file header is the “File Attribute Tag” which contains a property indicating the version of ActionScript in that SWF file

File Attribute Tag

It’s easy to edit the code of our class to read this property. One thing we should know that the type RECORDHEADER for tag “File Attribute” is the short one. That means its size is 2 bytes therefore we just need to read 2 bytes to bypass this struct. Then extract from the next byte the value of ActionScript version

int nTemp = strmReader.ReadByte();
nTemp = strmReader.ReadByte();
nTemp = strmReader.ReadByte();
ActionScript3 = ((nTemp & 8 ) == 0)?false:true;

Now through file attributes we can only define the ActionScript is version 3.0 or version 1.0/2.0. The complete source code of this blog can be downloaded as “Read SWF Header

UPDATE 11.02.2014
Mirror links for SWF specification format

Mirror 1: http://www.mediafire.com/view/iiowu0eg9gx3auo/swf-file-format-spec.pdf
Mirror 2: https://app.box.com/s/9vo2s1oa23hkjdd0ms7z
Mirror 3: https://mega.co.nz/#!2kR1laIC!k4sKKnOlpDNpubfsFCLWDbgU2iFWOjA2d3RgxyJsywg