Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / Visual Basic
Article

How to transform JPEG to SWF on the fly

Rate me:
Please Sign up or sign in to vote.
4.56/5 (9 votes)
20 Dec 20064 min read 139.1K   3.2K   58   29
How to transform JPEG to SWF on the fly.

Jpeg to Swf sample with C#

Goal of this article

This article's main goal is to show how to transform a JPEG file into a SWF animation on the fly, with a few code lines.

Using the SwfDotNet library

SwfDotNet library is a C# open source framework available at:

This library offers some good stuff to read about, and allows to write SWF format files in any version from 1 to 7. SwfDotNet.IO.dll is compatible with .NET framework 1.1, 2.0 or +, and Mono. This library respects the MacromediaTM SWF format specifications. You can read and download the Macromedia official specifications here.

Theory

First of all, I recommend you read my other article about SwfDotNet on CodeProject, "How to create a simple SWF decompiler with the SwfDotNet library". In this article, you can find generic information about how to use this library, and it's a good introduction to understand this article.

The SWF tags sequence

An SWF file is a sequence of specific tags which are used, for example, to embed data, to describe how to display library elements, add a frame, place an object, remove an object, etc.

Every tag describes a character on the screen, and has a character ID, which is composed of a unique number.

Tags used in this sample

Here is the list of tags which are necessary for this example:
  • SetBackgroundColorTag: to define the background color.
  • DefineBitsJpeg2Tag: to embed JPEG data in the SWF.
  • DefineShapeTag: to create a shape on the screen that will describe the way to display the DefineBitsJpeg2Tag data. Thanks to it, we can, for example, apply a transformation, etc.
  • PlaceObject2Tag: to place the DefineShapeTag object on the screen, with a location, a size, etc.
  • ShowFrameTag: to finally add a frame.

Creating the sample application

First, include the SwfDotNet library in your class:

C#
using SwfDotNet.IO;
using SwfDotNet.IO.Tags;
using SwfDotNet.IO.Tags.Types;

We will use the System.Drawing.Image class to load the JPEG file, and then we can transform BMP, GIF, PNG with the same code too:

C#
Image img = Image.FromFile(jpegFileName);
int posX = 0;
int posY = 0;
int imgWidth = img.Width;
int imgHeight = img.Height;

Now, we create a Swf object and fill the header information. Don't forget that the SWF format uses inches as unit, not pixels! (1 pixel = 20 inches.) To decrease the bandwidth, we choose to compress our final animation. To do so, add the following specific "CWS" signature:

C#
//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));

We can now import the JPEG data in our animation. It's really simple to do this with the SwfDotNet library, thanks to the FromImage static method on the DefineBitsJpeg2Tag object.

You can notice that a DefineBitsJpeg2 tag is a "character tag". Then, we must set a unique ID to this object. To do so, you can use the method GetNewDefineId on the swf object.

C#
//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 (not to display the jpeg)
swf.Tags.Add(DefineBitsJpeg2Tag.FromImage(jpegId, img));

Now comes the most difficult part of this sample: we're going to create the defineShape that will describe the way to display JPEG data.

First, we must create a new unique ID for this tag. Then, we specify the fill style of our shape, and use the FillStyleType.ClippedBitmapFill to fill the shape with the JPEG data. We must now define the drawing process in the ShapeRecordCollection object. The StyleChangeRecord class specifies that we will set the pen position at the upper left point of the bitmap. StraightEdgeRecord objects specify the bitmap rect co-ords, and finally, the EndShapeRecord closes the drawing process.

To conclude, we add a PlaceObject2Tag object to place the shape on the screen.

C#
//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));

At last, we will add a frame to display, and save our Swf object to a file, thanks to the SwfWriter class. You wan notice that the SwfWriter can write in a file or in a stream (for web applications, for example).

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

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

img.Dispose();

Conclusion

As you can see, thanks to the SwfDotNet library, you can easily create a JPEG to SWF program. Thanks to this code, it's easy to create a WebForm component, which creates a Flash slideshow from JPEG files and always on the fly.

We have seen how to draw a simple shape in an SWF file, but it's really only an introduction. We will further develop this topic in a future article.

History

  • 19/12/2006: First article version. Special thanks to Nicolas Guary and Pierre Cleaud for helping me with the translation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
Olivier Carpentier is a french C#.Net Architect, Microsoft Certified Professional.

Software Expert at SQLi, french consulting company
http://www.sqli.com

Comments and Discussions

 
QuestionHow do I write swf to byte array so i send over the network Pin
Member 780808414-Jun-15 20:25
Member 780808414-Jun-15 20:25 
Questionadd a button in flash file using SWFDotNet Pin
Bô Nhếch20-Aug-14 22:20
Bô Nhếch20-Aug-14 22:20 
QuestionHow to add a transparent PNG ? Pin
dxmedia13-Nov-11 11:45
dxmedia13-Nov-11 11:45 
GeneralAdd Multiple image with text and audio Pin
yogarajan_Ganesan16-Jun-11 19:52
yogarajan_Ganesan16-Jun-11 19:52 
QuestionHow could i convert .doc to .swf with swfdotnet library? Pin
swordfish23107-Sep-10 2:26
swordfish23107-Sep-10 2:26 
QuestionCan you put buttons or a trackback into the SWF? Pin
Member 440232126-Mar-10 8:32
Member 440232126-Mar-10 8:32 
GeneralSwf conversion Pin
Tapas Ranjan Singh7-Oct-09 0:42
Tapas Ranjan Singh7-Oct-09 0:42 
QuestionHow to control the Jpeg compression? Pin
abbabbabba11-Sep-09 3:49
abbabbabba11-Sep-09 3:49 
QuestionSlideshow/Actionscript? Pin
Terry Schofield19-May-09 11:32
Terry Schofield19-May-09 11:32 
QuestionEmbedded font Pin
Whim18-Jun-08 0:28
Whim18-Jun-08 0:28 
Generalyes but... Pin
garyemiller13-Mar-08 8:55
garyemiller13-Mar-08 8:55 
GeneralThanks Pin
sabrown10018-Nov-07 5:11
sabrown10018-Nov-07 5:11 
Questionhow to make a slideshow Pin
ghassen213-Aug-07 4:46
ghassen213-Aug-07 4:46 
QuestionCan it transfroms multi jpg files to a swf ? Pin
binbin30612-Jun-07 0:19
binbin30612-Jun-07 0:19 
AnswerRe: Can it transfroms multi jpg files to a swf ? Pin
Member 382817312-Jul-10 17:07
Member 382817312-Jul-10 17:07 
GeneralRe: Can it transfroms multi jpg files to a swf ? Pin
Rajesh M Panchal18-Sep-11 19:36
Rajesh M Panchal18-Sep-11 19:36 
AnswerRe: Can it transfroms multi jpg files to a swf ? Pin
Member 382817313-Jul-10 2:57
Member 382817313-Jul-10 2:57 
GeneralRe: Can it transfroms multi jpg files to a swf ? [modified] Pin
yogarajan_Ganesan16-Jun-11 19:01
yogarajan_Ganesan16-Jun-11 19:01 
GeneralRe: Can it transfroms multi jpg files to a swf ? Pin
miaodadao14-Mar-12 23:51
miaodadao14-Mar-12 23:51 
Thanks you for the RemoveObject2Tag.
But I can make it transparent still.
QuestionSwfDotNet library Pin
armen_stepanyan20-May-07 19:53
armen_stepanyan20-May-07 19:53 
GeneralUsing the SwfDotNet library Pin
armen_stepanyan20-May-07 19:52
armen_stepanyan20-May-07 19:52 
QuestionCan your SwfDotNet framework transfer WAV file to SWF? Pin
Vince Zhou28-Mar-07 18:59
Vince Zhou28-Mar-07 18:59 
QuestionCan your SwfDotNet framework transfer WAV file to SWF? Pin
Vince Zhou28-Mar-07 18:58
Vince Zhou28-Mar-07 18:58 
QuestionVS2003 Pin
Wayde16-Feb-07 11:08
Wayde16-Feb-07 11:08 
AnswerRe: VS2003 Pin
Olivier Carpentier18-Feb-07 23:47
Olivier Carpentier18-Feb-07 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.