问题描述
我已经创建了一个Flash文件,该文件能够通过单击每个受关注的按钮来创建影片剪辑.我想做的是,在定位所有创建的影片剪辑之后,我想将其保存到JPEG或PNG图像文件中.我创建了一个保存按钮,并将其命名为"save_page_btn".我尝试使用AS 2.0查找教程,但似乎无济于事.我在AS 3.0中没有基本知识.谁能帮助我找到此解决方案.
I have created a flash file which capable of creating movie clips by clicking each of respected buttons. What I want to do is, after I positioning all of these movie clips created, I would like to save it into JPEG or PNG image file. I have created a save button and named it "save_page_btn". I tried to find tutorial using AS 2.0 but it is seems to no avail. I have no basic in AS 3.0. Can anyone help me to find this solution.
谢谢大家!
推荐答案
ActionScript 2:
对于ActionScript 2,您必须使用服务器端脚本(例如我在.
For ActionScript 2, you have to save your image using a server side script, like a PHP script which I used in my answer for this question.
ActionScript 3:
对于ActionScript 3,事情变得更加简单,因为 FileReference
使我们能够将文件直接保存到我们的计算机中.
For ActionScript 3, things are more easy because FileReference
give us the ability to save a file directly to our machine.
因此,您需要将图像另存为jpg或png文件,可以使用中包含的 JPGEncoder
和 PNGEncoder
href ="https://github.com/mikechambers/as3corelib" rel ="nofollow noreferrer"> as3corelib 库.您可以从此处下载它,然后将其包含在项目中:文件>ActionScript设置...>库路径,然后按浏览到SWC文件标签,然后选择as3corelib.swc下载的文件.然后您可以这样做:
So for you case, you want to save your image as a jpg or a png file, which you can do using the JPGEncoder
and PNGEncoder
included in the as3corelib library. You can download it from here and then include it in your project from : File > ActionScript Settings ... > Library Path and then press the Browse to SWC file tab and select your as3corelib.swc downloaded file. Then you can do like this :
// in my stage, I have 2 buttons : btn_save_jpg and btn_save_png, and a MovieClip : movie_clip
import com.adobe.images.JPGEncoder;
import com.adobe.images.PNGEncoder;
btn_save_jpg.addEventListener(MouseEvent.CLICK, save_img);
btn_save_png.addEventListener(MouseEvent.CLICK, save_img);
function save_img(e:MouseEvent):void {
// verify which button is pressed using its name
var is_jpg:Boolean = (e.currentTarget.name).substr(-3, 3) == 'jpg';
// you can also write it : var is_jpg:Boolean = e.currentTarget === btn_save_jpg;
// create our BitmapData and draw within our movie_clip MovieClip
var bmd_src:BitmapData = new BitmapData(movie_clip.width, movie_clip.height)
bmd_src.draw(movie_clip);
if(is_jpg){
// if it's the btn_save_jpg button which is pressed, so create our JPGEncoder instance
// for the btn_save_png button, we don't need to create an instance of PNGEncoder
// because PNGEncoder.encode is a static function so we can call it directly : PNGEncoder.encode()
var encoder:JPGEncoder = new JPGEncoder(90);
// 90 is the quality of our jpg, 0 is the worst and 100 is the best
}
// get encoded BitmapData as a ByteArray
var stream:ByteArray = is_jpg ? encoder.encode(bmd_src) : PNGEncoder.encode(bmd_src);
// open the save dialog to save our image
var file:FileReference = new FileReference();
file.save(stream, 'snapshot_' + getTimer() + (is_jpg ? '.jpg' : '.png'));
}
如果您对AS3(甚至是AS2)代码有疑问,请随时在注释区域询问.
If you have a question about the AS3 ( or even the AS2 ) code, don't hesitate to ask it using the comments area.
希望可以提供帮助.
这篇关于屏幕截图并保存为JPG,Actionscript 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!