Welcome to MSDN Blogs Sign in | Join | Help

Example C# Code for Reading Flash (SWF) Files

Wow! it's been about 2 1/2 weeks since I last posted, and it seems like forever. In the meantime, I've upgraded my machines to Windows Vista RC2, and I'm not looking back. I had to do some work on my wife's computer the other day, and she's still running Windows XP. Well, not for long. :) Anyway...

Ever since my post about Converting Flash Shapes to WPF, I've been asked about the BitReader class that I mentioned. That class has since been replaced by a much more specialized class called SWFReader. As you can probably tell by the name, it's job is to make it easier to read Adobe/Macromedia Flash (SWF) files and their associated data types. If you're unfamiliar with the contents of a SWF file, you should check out A Concise Guide to the SWF File Format and Alexis' SWF Reference. Both are excellent resources.

To get going, first download my C# sample code: FlashTools.zip (10KB). This Visual Studio 2005 solution contains two projects:

  • The FlashTools project contains four classes: Rect.cs implements the Flash rectangle/RECT type, SWFFile.cs encapsulates logic to open a SWF file and read its contents, SWFReader.cs makes it easy to read common data types from a SWF file, and Tag.cs doesn't do much other than read and identify the various Flash tags.
  • The SWFInfo project is a very simple command-line client that routes tracing information to the console to display SWF header and tag information.

It's important to note that most modern Flash files use ZLIB compression (explained in RFCs 1950, 1951, and 1952). You could certainly implement your own "inflate" logic, but it's already been done by a number of folks. I chose to use the fully managed SharpZipLib. You'll need to download the assembly and add a reference to it from the FlashTools project to successfully compile my sample code.

To examine the contents of a SWF file in detail, the Dump Flash Decompiler is an invaluable tool. SWF Tools is another good one to check out (especially SWFDump). Both of these are useful for validating the data that you're reading from the SWF file.

Last, this is sample code, so swim at your own risk. There's no error checking, unit tests, performance optimizations, or guarantees. If you're going to build a more robust solution, this code will only give you enough guidance to get going. If you have any questions, feel free to contact me directly. Otherwise, party on!

Published Sunday, October 15, 2006 5:35 PM by mswanson
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# 收藏使用C#读取Flash(swf)文件, 生成WPF代码

Monday, October 16, 2006 10:02 AM by tongle

# SWF2XAML: A Tool to Convert Flash Files to XAML

Finally! After months of tinkering around in my spare time, I've managed to assemble a relatively useful

Tuesday, November 21, 2006 11:31 AM by Michael Swanson's Blog

# re: Example C# Code for Reading Flash (SWF) Files

Any chance of making it so you can write values? I'm looking for C# code to change framerate, width and height of a swf file.

Tuesday, November 28, 2006 11:18 PM by Chris Swenson

# re: Example C# Code for Reading Flash (SWF) Files

Chris: I don't have any need to update the code to write SWF files, and frankly, it would take a bit of work. Feel free to add the functionality yourself, or, you might want to check out Aspose.Flash for .NET (http://www.aspose.com/Products/Aspose.Flash/Default.aspx). I haven't tried their library, but it looks like it will both read and write SWF files.

Wednesday, November 29, 2006 2:50 AM by mswanson

# re: Example C# Code for Reading Flash (SWF) Files

Thanks, I've had something similar for delphi, but in moving to .net I've not been able to find anything, your's was close, this product seems to do what I need (plus about 1000 other things). ;-)

Thanks alot for pointing me to this!

Thursday, November 30, 2006 12:09 AM by Chris Swenson

# re: Example C# Code for Reading Flash (SWF) Files

Hey, can you use this to just get the dimensions of a swf file in C# code?

Friday, January 12, 2007 11:20 AM by Justin

# re: Example C# Code for Reading Flash (SWF) Files

Where can I find information about how to create a submission form, like the one I'm filling out now, that will work on an ASPX page (like this)?  It looks like the one that is being used here is Javascript.  How can I find out how to process?

I'm a noob at this, especially ASP.NET.  I really need to get a simple email form submission on my website though.  I'm having a very hard time finding clear info as to how.

Thanks!!!

Monday, February 05, 2007 3:31 PM by Kim

# re: Example C# Code for Reading Flash (SWF) Files

Kim: This was the wrong place to post a question like that. There are forums available that can help you with matters like that. Anyway, google the web for "Sending mail using C#" & see what you discover.

All the best

Thursday, May 17, 2007 11:47 PM by Raihan

# re: Example C# Code for Reading Flash (SWF) Files

Thank you very much for this!  It worked perfectly for my project.  I looked long and hard for a solution to this and yours is excellent.

Thursday, August 16, 2007 12:04 AM by Glenn

# VC++ code with 'replacing' the value of variables in SWF file

Dear Mike,

I found your great article, and I was able to make my own SWF tool which I can replace the value of 'Variables' (Specifically, the "EditText" object) in SWF files, in VC++ just in an hour.

Thank you so much... If I didn't see your article, I maybe had to waste my time for learning the SWF file structure and the implementation for Flash specific data types (especially the 'RECT')...

If you are ok, then I want to post my VC++ code to 'codeproject.com'.  It is basically just a conversion to the C++ language, and with just a little changes for implementing my new functions.

Please consider positively.

Best regards.

Tuesday, October 23, 2007 1:48 AM by brucewang-korea

# re: Example C# Code for Reading Flash (SWF) Files

Hi

Is it possible to extract a still picture from the swf file or flv ?

Thanks in Advance

deepumi@gmail.com

Tuesday, January 22, 2008 5:29 AM by Deepu

# thanks

thanks!

I was thinking of building a lib from my own.

Now I have a good place to start!

Monday, February 18, 2008 7:58 AM by one software developer

# SWF Decompiler

Hi Mike,

I am writing a Flash Decompiler to update a static text in the SWF file.I can extract  the tags using the SWFDotnet Library (http://sourceforge.net/projects/swfdotnet/) but I am having problem with tag DefineFont2 I cannot retrieve the Font Code in order to rebuild the text using the Glyph.

Please can you help figure out how to do so ?

Kind Regards

shukkux@gmail.com

Monday, May 26, 2008 12:25 PM by Koudjo

# Example C# Code for Reading Flash (SWF) Files

Hi Mike,

I'm interested in changing parameters in a SWF file, that way I can execute some sort of generic AS3 code while changing parameter values after rewriting the SWF.

Do you know what tag should I be looking for in the SWF format? Is it possible to change parameter values as I described?

ori@mentanet.com

All the best and thanks in advance,

Ori

Thursday, October 23, 2008 8:00 AM by Ori

# re: Example C# Code for Reading Flash (SWF) Files

@Ori...I haven't worked with the file format in over year now, and I don't recall the specific tags. If you follow the documentation links in this post, however, you should be able to find your answer. Good luck!

Thursday, October 23, 2008 11:36 AM by mswanson

# SWF Update DefineText2 Content

HI Mark

Please can you help me regarding the update of the definetext2 content.I can extract the text but cannot figure out how to update the text inside the tag.

Kind Regards

shukku

Friday, October 31, 2008 6:16 AM by shukku

# re: Example C# Code for Reading Flash (SWF) Files

Can you pls explain how to read metadata from flash files as it is being streamed to the browser?

Wednesday, November 05, 2008 8:31 PM by F3

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker