Reading width & height of Flash file

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 (?<width>\d+(\.\d+)?) -Y (?<height>\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

kick it on DotNetKicks.com


Add comment

After you have posted a comment, an email will be sent to the provided email address. Before your comment is activated, you will have to click the confirmation link within the email.

Name:

Email (only used for validation):

Website (optional):

Message:

Notify me when new comments are added:

Please type the following letters into the box below:  

Post!