Click here to Skip to main content
Click here to Skip to main content

How to transform JPEG to SWF on the fly

, 20 Dec 2006
Rate this:
Please Sign up or sign in to vote.
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:

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

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

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
http://www.sqli.com

Comments and Discussions

 
QuestionHow to add a transparent PNG ? Pinmemberdxmedia13-Nov-11 11:45 
GeneralAdd Multiple image with text and audio Pinmemberyogarajan16-Jun-11 19:52 
QuestionHow could i convert .doc to .swf with swfdotnet library? Pinmemberswordfish23107-Sep-10 2:26 
QuestionCan you put buttons or a trackback into the SWF? PinmemberMember 440232126-Mar-10 8:32 
GeneralSwf conversion PinmemberTapas Ranjan Singh7-Oct-09 0:42 
QuestionHow to control the Jpeg compression? Pinmemberabbabbabba11-Sep-09 3:49 
QuestionSlideshow/Actionscript? PinmemberTerry Schofield19-May-09 11:32 
QuestionEmbedded font PinmemberWhim18-Jun-08 0:28 
Generalyes but... Pinmembergaryemiller13-Mar-08 8:55 
GeneralThanks Pinmembersabrown10018-Nov-07 5:11 
Questionhow to make a slideshow Pinmemberghassen213-Aug-07 4:46 
QuestionCan it transfroms multi jpg files to a swf ? Pinmemberbinbin30612-Jun-07 0:19 
AnswerRe: Can it transfroms multi jpg files to a swf ? PinmemberMember 382817312-Jul-10 17:07 
GeneralRe: Can it transfroms multi jpg files to a swf ? PinmemberRajesh M Panchal18-Sep-11 19:36 
AnswerRe: Can it transfroms multi jpg files to a swf ? PinmemberMember 382817313-Jul-10 2:57 
GeneralRe: Can it transfroms multi jpg files to a swf ? [modified] Pinmemberyogarajan16-Jun-11 19:01 
GeneralRe: Can it transfroms multi jpg files to a swf ? Pinmembermiaodadao14-Mar-12 23:51 
QuestionSwfDotNet library Pinmemberarmen_stepanyan20-May-07 19:53 
GeneralUsing the SwfDotNet library Pinmemberarmen_stepanyan20-May-07 19:52 
QuestionCan your SwfDotNet framework transfer WAV file to SWF? PinmemberVince Zhou28-Mar-07 18:59 
QuestionCan your SwfDotNet framework transfer WAV file to SWF? PinmemberVince Zhou28-Mar-07 18:58 
QuestionVS2003 PinmemberWayde16-Feb-07 11:08 
AnswerRe: VS2003 PinmemberOlivier Carpentier18-Feb-07 23:47 
Generalwhere you provided the jpeg image name Pinmembergr8aqeel21-Dec-06 3:05 
GeneralRe: where you provided the jpeg image name PinmemberOlivier Carpentier21-Dec-06 3:16 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 20 Dec 2006
Article Copyright 2006 by Olivier Carpentier
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid