IMAGE TO SWF

Convert Images To SWF in C#

DOWNLOAD SOURCE HERE

Introduction:

In this article we will see how to convert our jpeg images to .swf format. For this task we are using some third party dll’s which will convert our images to one of the .swf movie. For viewing this movies you can view it by downloading this swf player.

Background:

Actually this things to convert images to swf I found on google search. In this article I’m using some dll files as given bellow to do our work.

1)  log4net

2)  SwfDotNet

3)  SharpZipLib

Follow the step given bellow to do our work.

Step 1:

Create one windows from application in C#. Add the reference to this dll’s which I have kept in bin folder of attached source as well Import this namespace in our using statements like.

using log4net;

using SwfDotNet.IO;

using SwfDotNet.IO.Tags;

using SwfDotNet.IO.Tags.Types;

 

Step2:

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

Step3:

In click of create button write following code to convert input file to .swf format and store it on 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.

2 responses to “IMAGE TO SWF

    • Suparb coding. Really Helpful. Thank u very very much 🙂

      SwfWriter writer = new SwfWriter(outputfilename);
      //Create a new Swf instance
      Swf swf = new Swf();

      string strPath = inputfilename;//@”D:\Cam\Cam\Stream_Images_All”;
      DirectoryInfo downloadedMessageInfo = new DirectoryInfo(strPath);
      foreach (FileInfo file in downloadedMessageInfo.GetFiles())
      {
      inputfilename = file.FullName;

      //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;

      //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, 5, 0, 0));

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

      img.Dispose();
      }
      //Write the swf to a file
      writer.Write(swf);
      writer.Close();
      MessageBox.Show(“File Created & Stored”);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s