Convert Images to SWF Format Using C#


Introduction:

In this article we will see how to convert our JPEG images to .swf format. For this we are using a third party's DLL which will convert our images to a .swf movie. For viewing the movies you can download the swf player.

Background:

Actually the DLL to convert images to swf I found using a Google search. In this article I'm using DLL files as given below to do our work.

  1. log4net
  2. SwfDotNet
  3. SharpZipLib

Follow the steps given below to do our work.

Step 1:

Create a Windows Form application in C#. Add the reference to the DLLs that I have put in the bin folder of the attached source as well a "using" for this namespace in our using statements like:

using log4net;
using SwfDotNet.IO;
using SwfDotNet.IO.Tags;
using SwfDotNet.IO.Tags.Types;

Step 2:

Design the normal form for taking input filename and output path for our work with OpenFile Dialog and SaveFile Dialog.

Step 3:

In the click of the create button write the following code to convert the input file to .swf format and store it to the specified location.

try
            {
                if (File.Exists(inputfilename) == false)
                {
                    MessageBox.Show("Can not open Image file" + inputfilename);

                    return;
                }

                //First load the picture as System.Drawing.Image
                Image img = Image.FromFile(inputfilename);
                int posX = 0;
                int posY = 0;
                int imgWidth = img.Width;
                int imgHeight = img.Height;

                //Create a new Swf instance
                Swf swf = new Swf();
                //Set size in inch unit (1 pixel = 20 inches)
                swf.Size = new Rect(0, 0, (posX + imgWidth) * 20, (posY + imgHeight) * 20);
                swf.Version = 7;    //Version 7 (for compression, must be > 5)
                swf.Header.Signature = "CWS";   //Set the signature to compress the swf

                //Set the background color tag as white
                swf.Tags.Add(new SetBackgroundColorTag(255, 255, 255));

                //Set the jpeg tag
                ushort jpegId = swf.GetNewDefineId();
                //Load the jped directly from an image
                //In fact, this line will load the jpeg data in the file as
                //a library element only 
                swf.Tags.Add(DefineBitsJpeg2Tag.FromImage(jpegId, img));

                //Now we will define the picture's shape tag
                //to define all the transformations on the picture (as rotation, color effects, etc..)
                DefineShapeTag shapeTag = new DefineShapeTag();
                shapeTag.CharacterId = swf.GetNewDefineId();
                shapeTag.Rect = new Rect(posX * 20 - 1, posY * 20 - 1, (posX + imgWidth) * 20 - 1, (posY + imgHeight) * 20 - 1);
                FillStyleCollection fillStyles = new FillStyleCollection();
                fillStyles.Add(new BitmapFill(FillStyleType.ClippedBitmapFill, ushort.MaxValue, new Matrix(0, 0, 20, 20)));
                fillStyles.Add(new BitmapFill(FillStyleType.ClippedBitmapFill, jpegId, new Matrix(posX * 20 - 1, posY * 20 - 1, (20.0 * imgWidth) / img.Width, (20.0 * imgHeight) / img.Height)));
                LineStyleCollection lineStyles = new LineStyleCollection();
                ShapeRecordCollection shapes = new ShapeRecordCollection();
                shapes.Add(new StyleChangeRecord(posX * 20 - 1, posY * 20 - 1, 2));
                shapes.Add(new StraightEdgeRecord(imgWidth * 20, 0));
                shapes.Add(new StraightEdgeRecord(0, imgHeight * 20));

                shapes.Add(new StraightEdgeRecord(-imgWidth * 20, 0));
                shapes.Add(new StraightEdgeRecord(0, -imgHeight * 20));
                shapes.Add(new EndShapeRecord());
                shapeTag.ShapeWithStyle = new ShapeWithStyle(fillStyles, lineStyles, shapes);
                swf.Tags.Add(shapeTag);

                //Place the picture to the screen with depth=1
                swf.Tags.Add(new PlaceObject2Tag(shapeTag.CharacterId, 1, 0, 0));

                //Add a single frame
                swf.Tags.Add(new ShowFrameTag());
                swf.Tags.Add(new EndTag());

                //Write the swf to a file
                SwfWriter writer = new SwfWriter(outputfilename);
                writer.Write(swf);
                writer.Close();

                img.Dispose();
                MessageBox.Show("File Created & Stored");

            catch (Exception ex)
            {
                MessageBox.Show("Error Occurred \n " + ex.Message.ToString());
            }
Conclusion:

In this simple way we can convert our image files to SWF.


Similar Articles