Partager via


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!

Comments

  • Anonymous
    October 16, 2006
    原文: http://blogs.msdn.com/mswanson/archive/2006/10/15/example-c-code-for-reading-flash-swf-files.aspx

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

  • Anonymous
    November 28, 2006
    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.

  • Anonymous
    November 28, 2006
    The comment has been removed

  • Anonymous
    November 29, 2006
    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!

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

  • Anonymous
    February 05, 2007
    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!!!

  • Anonymous
    May 17, 2007
    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

  • Anonymous
    August 15, 2007
    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.

  • Anonymous
    October 22, 2007
    The comment has been removed

  • Anonymous
    January 21, 2008
    Hi Is it possible to extract a still picture from the swf file or flv ? Thanks in Advance deepumi@gmail.com

  • Anonymous
    February 17, 2008
    thanks! I was thinking of building a lib from my own. Now I have a good place to start!

  • Anonymous
    May 26, 2008
    The comment has been removed

  • Anonymous
    October 23, 2008
    The comment has been removed

  • Anonymous
    October 23, 2008
    @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!

  • Anonymous
    October 30, 2008
    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

  • Anonymous
    November 05, 2008
    The comment has been removed

  • Anonymous
    February 27, 2009
    Hi Mike, This is just what i need to start on my project ^_- thanks for sharing G

  • Anonymous
    April 13, 2009
    The comment has been removed

  • Anonymous
    April 28, 2009
    Hi Mike, Yes this article is helpful to read the SFW Header.Can you please suggest how to read all the tags and there corresponding value and extract the informations embebded in that. Thanks in advance for your help Nil

  • Anonymous
    November 29, 2009
    Hi! thanks for that library. I tried several libraries to determine an swf size and all others return wrong values. But this library returns swf sizes well. Thanks!