Click here to Skip to main content
14,830,943 members
Articles / Programming Languages / Visual Basic
Posted 20 Dec 2006


58 bookmarked

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
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.


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:

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:

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:

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

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

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

//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).

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



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.


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


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


About the Author

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

Software Expert at SQLi, french consulting company

Comments and Discussions

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