Exemple #1
0
        public static void ConvertBmpToSwf(Bitmap bmp, string outputSwfFileName)
        {
            int   posX   = 0; //Posx
            int   posY   = 0; //Posy
            Image image  = bmp;
            int   width  = image.Width;
            int   height = image.Height;

            //自动缩小大图片
            if (width > 610)
            {
                double rw        = width;
                double rh        = height;
                double newheight = rh * 610 / rw;
                width  = 610;
                height = Convert.ToInt32(newheight);
                image  = image.GetThumbnailImage(width, height, null, IntPtr.Zero);
            }
            Swf swf = new Swf();

            swf.Size             = new Rect(0, 0, (posX + width) * 20, (posY + height) * 20);
            swf.Version          = 7;
            swf.Header.Signature = "CWS";
            swf.Tags.Add(new SetBackgroundColorTag(255, 255, 255));
            ushort newDefineId = swf.GetNewDefineId();

            swf.Tags.Add(DefineBitsJpeg2Tag.FromImage(newDefineId, image));
            DefineShapeTag tag = new DefineShapeTag();

            tag.CharacterId = swf.GetNewDefineId();
            tag.Rect        = new Rect((posX * 20) - 1, (posY * 20) - 1, ((posX + width) * 20) - 1, ((posY + height) * 20) - 1);
            FillStyleCollection fillStyleArray = new FillStyleCollection();

            fillStyleArray.Add(new BitmapFill(FillStyleType.ClippedBitmapFill, 0xffff, new Matrix(0, 0, 20.0, 20.0)));
            fillStyleArray.Add(new BitmapFill(FillStyleType.ClippedBitmapFill, newDefineId, new Matrix((posX * 20) - 1, (posY * 20) - 1, (20.0 * width) / ((double)image.Width), (20.0 * height) / ((double)image.Height))));
            LineStyleCollection   lineStyleArray = new LineStyleCollection();
            ShapeRecordCollection shapes         = new ShapeRecordCollection();

            shapes.Add(new StyleChangeRecord((posX * 20) - 1, (posY * 20) - 1, 2));
            shapes.Add(new StraightEdgeRecord(width * 20, 0));
            shapes.Add(new StraightEdgeRecord(0, height * 20));
            shapes.Add(new StraightEdgeRecord(-width * 20, 0));
            shapes.Add(new StraightEdgeRecord(0, -height * 20));
            shapes.Add(new EndShapeRecord());
            tag.ShapeWithStyle = new ShapeWithStyle(fillStyleArray, lineStyleArray, shapes);
            swf.Tags.Add(tag);
            swf.Tags.Add(new PlaceObject2Tag(tag.CharacterId, 1, 0, 0));
            swf.Tags.Add(new ShowFrameTag());
            swf.Tags.Add(new EndTag());
            SwfWriter writer = new SwfWriter(outputSwfFileName);

            writer.Write(swf);
            writer.Close();
            image.Dispose();
        }
        /// <summary>
        /// Creates a new <see cref="DefineMorphShapeTag"/> instance.
        /// </summary>
        /// <param name="characterId">Character id.</param>
        /// <param name="startBounds">Start bounds.</param>
        /// <param name="endBounds">End bounds.</param>
        /// <param name="offset">Offset.</param>
        /// <param name="morphFillStyles">Morph fill styles.</param>
        /// <param name="morphLineStyles">Morph line styles.</param>
        /// <param name="startEdges">Start edges.</param>
        /// <param name="endEdges">End edges.</param>
        public DefineMorphShapeTag(ushort characterId, Rect startBounds, Rect endBounds, uint offset,
                                   MorphFillStyleCollection morphFillStyles, MorphLineStyleCollection morphLineStyles, ShapeRecordCollection startEdges, ShapeRecordCollection endEdges)
        {
            this.characterId     = characterId;
            this.startBounds     = startBounds;
            this.endBounds       = endBounds;
            this.offset          = offset;
            this.morphFillStyles = morphFillStyles;
            this.morphLineStyles = morphLineStyles;
            this.startEdges      = startEdges;
            this.endEdges        = endEdges;

            this._tagCode = (int)TagCodeEnum.DefineMorphShape;
        }
        /// <summary>
        /// see <see cref="SwfDotNet.IO.Tags.BaseTag">base class</see>
        /// </summary>
        public override void ReadData(byte version, BufferedBinaryReader binaryReader)
        {
            RecordHeader rh = new RecordHeader();

            rh.ReadData(binaryReader);

            characterId = binaryReader.ReadUInt16();
            binaryReader.SynchBits();

            startBounds = new Rect();
            startBounds.ReadData(binaryReader);

            binaryReader.SynchBits();
            endBounds = new Rect();
            endBounds.ReadData(binaryReader);
            binaryReader.SynchBits();

            offset = binaryReader.ReadUInt32();

            morphFillStyles = new MorphFillStyleCollection();
            morphFillStyles.ReadData(binaryReader);

            morphLineStyles = new MorphLineStyleCollection();
            morphLineStyles.ReadData(binaryReader);

            ShapeWithStyle.NumFillBits = (uint)morphFillStyles.Count;
            ShapeWithStyle.NumLineBits = (uint)morphLineStyles.Count;

            startEdges = new ShapeRecordCollection();
            startEdges.ReadData(binaryReader, ShapeType.None);

            ShapeWithStyle.NumFillBits = (uint)morphFillStyles.Count;
            ShapeWithStyle.NumLineBits = (uint)morphLineStyles.Count;

            endEdges = new ShapeRecordCollection();
            endEdges.ReadData(binaryReader, ShapeType.None);
        }
Exemple #4
0
        static void Main(string[] args)
        {
            //Picture to transform
            string imgPath = "img.jpg";
            string path    = "test_alpha.swf";
            //Alpha translation informations
            int alphaFrameNum = 35;  //frame duration
            int alphaStart    = 10;  //alpha percent start
            int alphaEnd      = 100; //alpha percent end

            //Load the picture to a GDI image
            Image img       = Image.FromFile(imgPath);
            int   posX      = 0;
            int   posY      = 0;
            int   imgWidth  = img.Width / 2;
            int   imgHeight = img.Height / 2;

            //Create a new Swf instance
            Swf swf = new Swf();

            swf.Size    = new Rect(0, 0, (posX + imgWidth) * 20, (posY + imgHeight) * 20);
            swf.Version = 5;

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

            //Set the jpeg tag
            ushort jpegId = swf.GetNewDefineId();

            //Load the jped from an image
            swf.Tags.Add(DefineBitsJpeg2Tag.FromImage(jpegId, img));

            //Define the picture's shape tag
            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
            swf.Tags.Add(new PlaceObject2Tag(shapeTag.CharacterId, 1, 0, 0));
            //Add a frame
            swf.Tags.Add(new ShowFrameTag());

            for (int i = 0; i < alphaFrameNum; i++)
            {
                int percent    = (i * 100) / alphaFrameNum;
                int diff       = alphaEnd - alphaStart;
                int valPercent = (diff * percent) / 100 + alphaStart;
                int valRgb     = (255 * valPercent) / 100;
                swf.Tags.Add(new PlaceObject2Tag(1, new CXFormWithAlphaData(256, 256, 256, valRgb)));
                swf.Tags.Add(new ShowFrameTag());
            }
            swf.Tags.Add(new EndTag());

            //Write the swf to a file
            SwfWriter writer = new SwfWriter(path);

            writer.Write(swf);
            writer.Close();
            img.Dispose();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            //Picture to transform
            string imgPath = "img.jpg";
            //string imgPath = "Untitled-1.bmp";
            //File name of the result swf file
            string path = "test.swf";

            //Load the picture to a GDI image
            Image img       = Image.FromFile(imgPath);
            int   posX      = 0;
            int   posY      = 0;
            int   imgWidth  = img.Width + 100;
            int   imgHeight = img.Height + 100;

            //Create a new Swf instance
            Swf swf = new Swf();

            swf.Size    = new Rect(0, 0, (posX + imgWidth) * 20, (posY + imgHeight) * 20);
            swf.Version = 5;

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

            //Set the jpeg tag
            ushort jpegId = swf.GetNewDefineId();

            //Load the jped from an image
            swf.Tags.Add(DefineBitsJpeg2Tag.FromImage(jpegId, img));

            //Define the picture's shape tag
            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
            swf.Tags.Add(new PlaceObject2Tag(shapeTag.CharacterId, 1, 0, 0));
            //Add a frame
            swf.Tags.Add(new ShowFrameTag());
            swf.Tags.Add(new EndTag());

            //Write the swf to a file
            SwfWriter writer = new SwfWriter(path);

            writer.Write(swf);
            writer.Close();
            img.Dispose();
        }
        /// <summary>
        /// see <see cref="SwfDotNet.IO.Tags.BaseTag">base class</see>
        /// </summary>
        public override void UpdateData(byte version)
        {
            if (version < 2)
            {
                return;
            }

            bool fontFlagsWideOffsets = HasWideOffsets();
            bool fontFlagsHasLayout   = HasLayoutInfo();
            bool fontFlagsWideCodes   = HasWideCodes(version);

            if (glyphShapesTable != null)
            {
                glyphShapesTable.IsWideCodes = fontFlagsWideCodes;
            }

            MemoryStream         m = new MemoryStream();
            BufferedBinaryWriter w = new BufferedBinaryWriter(m);

            RecordHeader rh = new RecordHeader(TagCode, GetSizeOf());

            rh.WriteTo(w);

            w.Write(fontId);

            w.WriteBoolean(fontFlagsHasLayout);
            w.WriteBoolean(fontFlagsShiftJIS);
            w.WriteBoolean(fontFlagsSmallText);
            w.WriteBoolean(fontFlagsANSI);
            w.WriteBoolean(fontFlagsWideOffsets);
            w.WriteBoolean(fontFlagsWideCodes);
            w.WriteBoolean(fontFlagsItalic);
            w.WriteBoolean(fontFlagsBold);
            if (version >= 6)
            {
                w.Write((byte)languageCode);
            }
            else
            {
                w.Write((byte)0);
            }
            w.Write((byte)(this.fontName.Length + 1));
            w.WriteString(fontName);

            int numGlyph = GetNumGlyphs();

            w.Write((ushort)numGlyph);

            glyphShapesTable.IsWideCodes = fontFlagsWideCodes;

            //Create the codetableoffset and offsettable
            int offsetTableSize = 0;

            if (fontFlagsWideOffsets)
            {
                offsetTableSize = (numGlyph * 4) + 4;
            }
            else
            {
                offsetTableSize = (numGlyph * 2) + 2;
            }

            char[]      codes         = glyphShapesTable.GetOrderedCodes();
            IEnumerator glyphsEnum    = glyphShapesTable.GetOrderedGlyphs(codes).GetEnumerator();
            int         currentOffset = 0;

            for (int i = 0; glyphsEnum.MoveNext(); i++)
            {
                long offset = offsetTableSize + currentOffset;
                if (fontFlagsWideOffsets)
                {
                    w.Write((uint)offset);
                }
                else
                {
                    w.Write((ushort)offset);
                }

                ShapeRecordCollection shapes = ((ShapeRecordCollection)glyphsEnum.Current);
                int shapeSize = shapes.GetSizeOf();
                currentOffset += shapeSize;
            }

            if (fontFlagsWideOffsets)
            {
                w.Write((uint)(offsetTableSize + currentOffset));
            }
            else
            {
                w.Write((ushort)(offsetTableSize + currentOffset));
            }

            glyphShapesTable.WriteTo(w);

            if (fontFlagsHasLayout)
            {
                w.Write(fontAscent);
                w.Write(fontDescent);
                w.Write(fontLeading);

                if (numGlyph > 0)
                {
                    fontAdvanceTable.WriteTo(w);
                    fontBoundsTable.WriteTo(w);
                    if (version >= 7)
                    {
                        w.Write((ushort)0);
                    }
                    else
                    {
                        fontKerningTable.WriteTo(w);
                    }
                }
            }

            w.Flush();
            // write to data array
            _data = m.ToArray();
        }
Exemple #7
0
        static void Main(string[] args)
        {
            //Picture to transform
            string imgPath = "img.jpg";
            string path    = "test_color.swf";
            //Alpha translation informations
            int colorEffectFrameNum = 20; //frame duration

            System.Drawing.Color startColor = System.Drawing.Color.Yellow;
            System.Drawing.Color endColor   = System.Drawing.Color.Black;

            //Load the picture to a GDI image
            Image img       = Image.FromFile(imgPath);
            int   posX      = 0;
            int   posY      = 0;
            int   imgWidth  = img.Width / 2;
            int   imgHeight = img.Height / 2;

            //Create a new Swf instance
            Swf swf = new Swf();

            swf.Size    = new Rect(0, 0, (posX + imgWidth) * 20, (posY + imgHeight) * 20);
            swf.Version = 5;

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

            //Set the jpeg tag
            ushort jpegId = swf.GetNewDefineId();

            //Load the jped from an image
            swf.Tags.Add(DefineBitsJpeg2Tag.FromImage(jpegId, img));

            //Define the picture's shape tag
            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
            swf.Tags.Add(new PlaceObject2Tag(shapeTag.CharacterId, 1, 0, 0));
            //Add a frame
            swf.Tags.Add(new ShowFrameTag());

            for (int i = 0; i < colorEffectFrameNum; i++)
            {
                int red   = GetRGBValue(i, startColor.R, endColor.R, colorEffectFrameNum);
                int green = GetRGBValue(i, startColor.G, endColor.G, colorEffectFrameNum);
                int blue  = GetRGBValue(i, startColor.B, endColor.B, colorEffectFrameNum);
                int alpha = GetRGBValue(i, startColor.A, endColor.A, colorEffectFrameNum);
                swf.Tags.Add(new PlaceObject2Tag(1, new CXFormWithAlphaData(red, green, blue, alpha)));
                swf.Tags.Add(new ShowFrameTag());
            }
            swf.Tags.Add(new EndTag());

            //Write the swf to a file
            SwfWriter writer = new SwfWriter(path);

            writer.Write(swf);
            writer.Close();
            img.Dispose();
        }
Exemple #8
0
        /// <summary>
        /// 生成flash
        /// </summary>
        /// <param name="imgFilePaths"></param>
        /// <param name="saveSwfFilePath"></param>
        /// <returns></returns>
        public static bool CreateSwf(List <string> imgFilePaths, string saveSwfFilePath)
        {
            try {
                Swf swf = new Swf();
                foreach (var item in imgFilePaths)
                {
                    if (!new FileInfo(item).Exists)
                    {
                        continue;
                    }
                    Image img = Image.FromFile(item);
                    img = img.GetThumbnailImage(img.Width / 2, img.Height / 2, null, IntPtr.Zero);
                    int posX      = 0;
                    int posY      = 0;
                    int imgWidth  = img.Width;
                    int imgHeight = img.Height;
                    //Create a new Swf instance
                    //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 (not to display the jpeg)
                    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));
                    swf.Tags.Add(new ShowFrameTag());
                    swf.Tags.Add(new RemoveObject2Tag(1)); //THE ADDED LINE!!//
                    img.Dispose();
                }
                //Write the swf to a file
                SwfWriter writer = new SwfWriter(saveSwfFilePath);
                writer.Write(swf);
                writer.Close();
                return(true);
            } catch {
                return(false);
            }
        }