Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# - Embed Flash loaded directly from a binary memory stream!
#1
I saw leaky back on business, So I got excited to see some coders back to the boring css community :Smile,
& decided to share this new tut with you all.

Ok I'm gonna show you how to embed flash movies (.SWF) to your application without needing the actual .swf file, because were gonna embed that to the (.EXE) assembly, & then load it out directly from the memory, YEH THAT"S AWESOME RIGHT!

So that might seem impossible especially using CLR, but it's not.

I made this little app for my friend, It's similar to an interactive business card, so I just edited the texts, and hid my name.

[Image: 5hskiowc8ltw63kqrca8.png]
This is how the main form would look like After compiling and running, when press push a form with all contact info appears,  The video is just a sample music video.


[/hr]

We need to add our .SWF movie, and in this case it's called "xeno.swf" as you can see plus other images & stuff.

[Image: x2eni46ajtd251gmgzit.png]

& we choose embed resource to each embed file:
[Image: 5sfda8dk6iys1ci4hv4.png]


[/hr]

Now we reference the following:

[Image: tpndtm5b4bapsclnlpvn.png]


[/hr]

Now we use this awesome class Code, very unique way to load .swf from a memory binary stream

Code:
private void InitFlashMovie(AxShockwaveFlashObjects.AxShockwaveFlash flashObj, byte[] swfFile)
        {
            using (MemoryStream stm = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(stm))
                {
                    /* Write length of stream for AxHost.State */
                    writer.Write(8 + swfFile.Length);
                    /* Write Flash magic 'hohoho' */
                    writer.Write(0x55665566);
                    /* Length of swf file */
                    writer.Write(swfFile.Length);
                    writer.Write(swfFile);
                    stm.Seek(0, SeekOrigin.Begin);
                    /* 1 == IPeristStreamInit */
                    flashObj.OcxState = new AxHost.State(stm, 1, false, null);
                }
            }
        }

& ofcourse we use it like this:
Code:
byte[] data = Properties.Resources.xeno;
InitFlashMovie(axShockwaveFlash1, data);

We load the movie from the embed resource into a byte array, & then we load the array as into the class, & the class buffers it into the flashobject directly, awesome right.

Here is the full code to my little project, whats important about it, is the 3 dynamic link library, which are needed to run the app, but we can't just leave them out because it wouldn't be professional, so I figured out a way to embed them to the .exe assembly & then when you run it, the app will install them to the directory where the app is placed, which will make the app much more dynamic and smooth.

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace TGF_VC
{
    public partial class Form1 : Form
    {
        //*------- app directory
        string directory = AppDomain.CurrentDomain.BaseDirectory;
        //*------- app directory
       

        public Form1()
        {


            try
            {
                //Checking Files...
                if (File.Exists(directory + @"\Interop.ShockwaveFlashObjects.dll"))
                {}
                else
                {
                    var bw = new BinaryWriter(File.Open(directory + @"\Interop.ShockwaveFlashObjects.dll", FileMode.OpenOrCreate));
                    bw.Write(Properties.Resources.Interop_ShockwaveFlashObjects);
                }
                //Checking Files...
                if (File.Exists(directory + @"\AxInterop.ShockwaveFlashObjects.dll"))
                {}
                else
                {
                    var bw1 = new BinaryWriter(File.Open(directory + @"\AxInterop.ShockwaveFlashObjects.dll", FileMode.OpenOrCreate));
                    bw1.Write(Properties.Resources.AxInterop_ShockwaveFlashObjects);
                }
                //Checking Files...
                if (File.Exists(directory + @"\Flash32_11_2_202_183.ocx"))
                {}
                else
                {
                    var bw2 = new BinaryWriter(File.Open(directory + @"\Flash32_11_2_202_183.ocx", FileMode.OpenOrCreate));
                    bw2.Write(Properties.Resources.Flash32_11_2_202_183);
                }
               
                    InitializeComponent();
               
               
            }
            catch
            {
                MessageBox.Show("Installed Successfully, Please Close & restart Restart.");
               
            }
        }
   
        private void Form1_Load(object sender, EventArgs e)
        {
           
            //-----------------------------------------------------------------------------------------

                byte[] data = Properties.Resources.xeno;
                InitFlashMovie(axShockwaveFlash1, data);
           
           
        }
        private void InitFlashMovie(AxShockwaveFlashObjects.AxShockwaveFlash flashObj, byte[] swfFile)
        {
            using (MemoryStream stm = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(stm))
                {
                    /* Write length of stream for AxHost.State */
                    writer.Write(8 + swfFile.Length);
                    /* Write Flash magic 'hohoho' */
                    writer.Write(0x55665566);
                    /* Length of swf file */
                    writer.Write(swfFile.Length);
                    writer.Write(swfFile);
                    stm.Seek(0, SeekOrigin.Begin);
                    /* 1 == IPeristStreamInit */
                    flashObj.OcxState = new AxHost.State(stm, 1, false, null);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AboutBox1 sdd = new AboutBox1();
            sdd.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}

As you can see, The code checks if the library's exist in the base directory, & if they don't it copy's them from it's self to the hard drive, But in order to load them to the memory, it needs to restart it's self, so simply closing it & reopening it would work.

then it "initiallizecomponent();"

then on formload() it loads the movie to a byte array & starts playing the movie with loop enabled ofcourse. That's it.
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]

Messages In This Thread
C# - Embed Flash loaded directly from a binary memory stream! - by MindHACKer - Apr 27 2012, 08:23 PM

Users browsing this thread: 1 Guest(s)