Embed flash as a resource in C#

Embedding a flash file into the project as a resource instead of content is easier then it seems. I won’t go into how to play flash files in C# but I will show you how to embed the file within the .exe.

/// <summary>
/// Loads a flash file into a AxShockwaveFlash from a resource.
/// </summary>
/// <param name="FlashObj">Flash control.</param>
/// <param name="Path">Path to swf, must be a resource.</param>
public static void LoadFlash(AxShockwaveFlash FlashObj, string Path)
{
	StreamResourceInfo resource = App.GetResourceStream(new Uri(Path, UriKind.Relative)); // Get resource

	using (resource.Stream)
	{
		if (resource.ContentType != "application/x-shockwave-flash") // Check resource is a swf file
		{
			throw new FileFormatException("Path must be a valid swf.");
		}

		byte[] data = new byte[resource.Stream.Length]; // Create a new byte array to hold the data
		resource.Stream.Read(data, 0, (int)resource.Stream.Length); // Read the data from the stream
		LoadFlash(FlashObj, data);
	}
}

/// <summary>
/// Loads a flash file into a AxShockwaveFlash from a byte array.
/// </summary>
/// <param name="FlashObj">Flash control.</param>
/// <param name="SwfFile">Byte array consiting of swf contents.</param>
public static void LoadFlash(AxShockwaveFlash FlashObj, byte[] SwfFile)
{
	// Taken from https://stackoverflow.com/questions/1874077/loading-a-flash-movie-from-a-memory-stream-or-a-byte-array/1994278#1994278
	using (MemoryStream stream = new MemoryStream())
	using (BinaryWriter writer = new BinaryWriter(stream))
	{
		writer.Write(8 + SwfFile.Length); // Write length of stream
		writer.Write(0x55665566); // Write Flash magic 'fUfU'
		writer.Write(SwfFile.Length); // Length of swf file
		writer.Write(SwfFile); // Write swf file
		stream.Seek(0, SeekOrigin.Begin); // Seek to start of stream
		FlashObj.OcxState = new AxShockwaveFlash.State(stream, 1, false, null);
	}
}

Usage: LoadFlash(axShockwaveFlash1, "MyFile.swf"). Set the Build Action to Resource on the .swf and you’re done.