码迷,mamicode.com
首页 > Web开发 > 详细

SwfDotNet的使用,多幅图片生成SWF

时间:2015-06-09 13:41:56      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

突然要做一个将多幅图片拼成一个swf的工作,而且还是用没有用过的C#,还好语法基本和java类似,网上找了一圈,找到一个SwfDotNet的插件,网址如下:

http://www.codeproject.com/KB/graphics/jpeg2swf.aspx?fid=369197&df=90&mpp=10&noise=3&prof=True&sort=Position&view=Quick&fr=21

页面上有示例代码,但仅针对一副图片,看不出来swf的效果,以下代码大部分使用示例代码,关键代码我会注释说明。

Image img = Image.FromFile(jpegFileName);
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
swf.Header.Fps = 1;//每秒多少帧,这个很重要,加入多幅图片后,依靠这个控制速度 
//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));
//Add a single frame
swf.Tags.Add(new ShowFrameTag());//这个也很重要,按照前面设置的fps参数,每秒1帧,重复多设置几帧,这个图片就能持续显示几秒
swf.Tags.Add(new ShowFrameTag());
swf.Tags.Add(new ShowFrameTag());//我这里重复了3次,就可以显示3秒了
swf.Tags.Add(new RemoveObject2Tag(1));//这个也很重要,前面的图片插入到了哪个层,就把哪个层移除掉,类似于图片叠加置前置后的那种
//以下为添加第二幅图片
image = System.Drawing.Image.FromFile("2.jpg");
imgid = swf.GetNewDefineId();
shapeTag = new DefineShapeTag();
shapeTag.CharacterId = swf.GetNewDefineId();
shapeTag.Rect = new SwfDotNet.IO.Tags.Types.Rect(posX * 20 - 1, posY * 20 - 1,(posX + imageWidth) * 20 - 1, (posY + imageWidth) * 20 - 1);
fillStyles = new FillStyleCollection();
fillStyles.Add(new SwfDotNet.IO.Tags.Types.BitmapFill(FillStyleType.ClippedBitmapFill, ushort.MaxValue, new SwfDotNet.IO.Tags.Types.Matrix(0, 0, 20, 20)));
fillStyles.Add(new BitmapFill(FillStyleType.ClippedBitmapFill, imgid, new SwfDotNet.IO.Tags.Types.Matrix(posX * 20 - 1, posY * 20 - 1,(20.0 * imageWidth) / image.Width,(20.0 * imageHeight) / image.Height)));
lineStyles = new LineStyleCollection(); shapes = new ShapeRecordCollection();
shapes.Add(new StyleChangeRecord(posX * 20 - 1, posY * 20 - 1, 2));
shapes.Add(new StraightEdgeRecord(imageWidth * 20, 0));
shapes.Add(new StraightEdgeRecord(0, imageHeight * 20));
shapes.Add(new StraightEdgeRecord(-imageWidth * 20, 0));
shapes.Add(new StraightEdgeRecord(0, -imageHeight * 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,将图片在屏幕中的的深度设置为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(outputSwfFileName); writer.Write(swf); writer.Close(); img.Dispose();
好了,总算把这个问题解决了,记录下,百度空间竟然关闭了,记录到这里好了!

SwfDotNet的使用,多幅图片生成SWF

标签:

原文地址:http://www.cnblogs.com/louiscuti/p/4563073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!