在之前的 Flash 部分我们已经了解了如何使用FLASH和摄像头相结合截取相片,但是实际上还没保存到物理路径中。这部分将为大家介绍如何将截取下来的相片保存成JPG图片。
第一步,在FLASH中,生成像素数组。
在主场景中增加一段ACTIONSCRIPT函数,用于判断数据是否成功发送到C#的代码中。代码如下:
function LoginResult1(success) {
if (success) {
trace(dataLoader.done)
return ("");
}
}
修改场景中截取相片的功能按钮的ACTIONSCRIPT,修改成以下代码
on (release)
{
_root.myBitmap.draw(local_video);//将场景中 视频组件 的内容,绘画在图形处理对象中
_root.tempObj.attachBitmap(_root.myBitmap, 1, "always", true);//将图形处理对象的内容,绘画到用于显示的 电影实例 中
//建立 loadvars 对象,用于发送和接收提交的数据
imagesend = new LoadVars();
//建立像素数组,存放相片每个像素的颜色像素值
imagesend.pixels_arr = "";
imagesend.pixels_width = int(_root.local_video._width-1);
imagesend.pixels_height = int(_root.local_video._height-1);
//循环获取像素
for ( xx = 0 ; xx < _root.local_video._width-1 ; xx++ ) {
for ( yy = 0 ; yy < _root.local_video._height-1 ; yy++ ) {
if(imagesend.pixels_arr == "")
{
imagesend.pixels_arr = imagesend.pixels_arr + _root.myBitmap.getPixel( xx , yy ).toString(16)
}
else
{
imagesend.pixels_arr = imagesend.pixels_arr + "," + _root.myBitmap.getPixel( xx , yy ).toString(16)
}
}
}
imagesend.onLoad = _root.LoginResult1;
imagesend.sendAndLoad("WebForm1.aspx", imagesend, "POST");//发送数据
}
第二步,利用C#的System.Drawing.Imaging类生成JPG
建立项目,新建一个页面 WebForm1.aspx 。 在 Page_Load 事件中,添加以下代码
string arr = Request["pixels_arr"].ToString();
int width = Convert.ToInt32(Request["pixels_width"].ToString());
int height = Convert.ToInt32(Request["pixels_height"].ToString());
Save2Pic(arr,width,height);
再添加一个生成图片的函数
private void Save2Pic(string PixelArray,int W,int H)
{
string delimStr = ",";
char [] delimiter = delimStr.ToCharArray();
string [] split = null;
split = PixelArray.Split(delimiter);
Bitmap image=new Bitmap (W,H);
int i=0;
for(int x=0;x<W;x++)
for(int y=0;y<H;y++)
{
image.SetPixel (x,y,Color.FromArgb (int.Parse (split.GetValue(i).ToString(), System.Globalization.NumberStyles.AllowHexSpecifier)));
i++;
}
image.Save (Server.MapPath (".") + "/crl" + DateTime.Now .ToString ("yyyyMMddhhmmss") + ".jpg",ImageFormat.Jpeg );
image.Dispose ();
}
这样,就完成了C# + Flash 制作大头贴 的全部基础功能了。当然了,还有很多细节上要做适当调整的。这个只是提供基础代码给各位参考而已。