as2+.net上传图片截图

这随笔记录flash actionscript2 实现截取一张载图并上传的.net服务器。

 

flash 代码

1.引用

import flash.display.BitmapData;
import flash.geom.Rectangle;

2.创建截图

var bmpScreen:BitmapData = new BitmapData(_root._width,_root._height);
bmpScreen.draw(_root); //我这里将整个屏画到BitmapData
//矩形为要截取区域
var imgWidth = 300;
var imgHeight = 200;
var bmpScreencap:BitmapData=new BitmapData(imgWidth,imgHeight);
bmpScreencap.copyPixels(bmpScreen,new Rectangle(40,40,imgWidth,imgHeight),new Point(0,0));

3. 将截图转为十六进制的颜色编码(hex-code)

var xPixel=0;
var yPixel=0;
var pointColor=0;
var newFxiedPointColor=0;
var mpluses=0;
var strBytesString=new String();
for(xPixel =0; xPixel < imgWidth; xPixel ++){
for(yPixel =0; yPixel < imgHeight; yPixel ++){
pointColor = bmpScreencap.getPixel(xPixel, yPixel).toString(16);//获取每个象素的Hex值
newFxiedPointColor =pointColor ;
for (mpluses=0;mpluses < 6 - pointColor.length;mpluses++){
newFxiedPointColor="0" + newFxiedPointColor; //不足6位要补足
}
strBytesString += newFxiedPointColor + ","; //我这里用逗号分隔每个象素的颜色值
};
};

4.上传到服务器

var lvDataSender:LoadVars = new LoadVars(); 
//设置上传参数
lvDataSender.imgwidth=imgWidth; //截图宽度
lvDataSender.imgheight=imgHeight; //载图高度
lvDataSender.imgrgb= strBytesString; //截图的Hex字符串
lvDataSender.imgurl=""; //定义接收参数
lvDataSender.sendAndLoad(“你服务器的api地址”, lvDataSender, "POST");
lvDataSender.onLoad = function(success:Boolean) {
if (success) {
ExternalInterface.call("saveImage",lvDataSender.imgurl); //成功后,处理你的逻辑,我这里调用外部javascript的saveImage方法
} else {
trace("ops :S"); // 失败
};
};

.net(C#)代码

1.后台服务影响,我这里用ashx文件

/// <summary>
/// 上传并保存图片
/// </summary>
public class Upload : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string result = string.Empty;
//获取参数
int imgWidth = int.Parse(context.Request.Form["imgwidth"].ToString());
int imgHeight = int.Parse(context.Request.Form["imgheight"].ToString());
string imgRGB = context.Request.Form["imgrgb"];
string[] strRgb = imgRGB.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

using(Bitmap bitmap = CreateBitmap(imgWidth, imgHeight, strRgb))
{
//保存图片
bitmap.Save(保存路径)
result = "" //设置反回值;
}
context.Response.ContentType = "text/plain";
context.Response.Write("imgurl="+result);
}

/// <summary>
/// 创建bitmap
/// </summary>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="rgb">rgb数组</param>
/// <returns></returns>
private Bitmap CreateBitmap(int width,int height,string[] rgb)
{
Bitmap bitmap = new Bitmap(width,height);
int index = 0;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++) {
string s = rgb[index];
int r=Convert.ToInt32(s.Substring(0,2),16);
int g = Convert.ToInt32(s.Substring(2, 2), 16);
int b = Convert.ToInt32(s.Substring(4, 2), 16);

bitmap.SetPixel(x, y, Color.FromArgb(r,g,b));
index++;
}
}
return bitmap;
}

public bool IsReusable
{
get
{
return false;
}
}
}



参考:Send And Upload Binary Bytes or Image from Flash with ActionScript+2.0



你可能感兴趣的:(.net)