improve.dk
Just another mindless drone looking for the perfect stack
posts - 215, comments - 453

Reading width & height of Flash file

Written on November 11, 2009 by Mark S. Rasmussen in Development: .NET, Development: AS/Flex/Flash

Obtaining the movie height & weidth of a Flash file is an easy task using the swfdump tool that comes as part of the swftools package. Here's an example of how to invoke swfdump from C# and read out the height & width of a given Flash file.

Start out by downloading on of the swftools releases. I'm using the latest development snapshot. I'll be using one of the Flash files I made in a previous blog post as a test file, but you can use any .swf file you want. The test Flash file is called test.swf.

Once you've extracted the swftools package and copied both the test file and the swfdump.exe file into your solution directory, we can now test it out manually:

D:\Webmentor Projekter\Blog\RetrievingSwfProperties\RetrievingSwfProperties>swfdump -X -Y test.swf
-X 500 -Y 375

By providing the -X and -Y switches swfdump only prints out the movie height & width. You can see all the switches on the swfdump man page. At this point it's a simple matter of spinning up a swfdump process and parsing the output:

static void Main(string[] args)
{
    // Set process properties
    Process p = new Process();
    p.StartInfo = new ProcessStartInfo("swfdump.exe", "-X -Y test.swf");
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.UseShellExecute = false;
    p.Start();

    // Read all output, waiting for process to end
    string output = p.StandardOutput.ReadToEnd();

    // Regex that'll match both the width and height output - has to take care of potential decimals
    Match m = Regex.Match(output, @"-X (?\d+(\.\d+)?) -Y (?\d+(\.\d+)?)");

    // Convert width & height to doubles forcing en-US culture
    double width = Convert.ToDouble(m.Groups["width"].Value, new CultureInfo("en-US"));
    double height = Convert.ToDouble(m.Groups["height"].Value, new CultureInfo("en-US"));

    Console.WriteLine("Width: " + width);
    Console.WriteLine("Height: " + height);
    Console.Read();
}

Result:

Width: 500
Height: 375

Feedback

No comments posted yet.

Post Comment

Name  
Email
Url
Comment
Please add 2 and 7 and type the answer here: