By Minidxer | April 26, 2008

 

 

在Adobe Flex 3中,你可以通过下面的代码获取任何控件并保存为bitmap图片(需要import “mx.graphics.ImageSnapshot”,想必大名鼎鼎的snapshot大家都应该知道吧?)。

  1. var snapshot:ImageSnapshot = ImageSnapshot.captureImage(backgroundCanvas);

默认的话,使用的是PNG格式的编码。如果你想将图片传递到服务器端,那就用下面的代码:

 

 

  1. var req:URLRequest = new URLRequest();
  2. req.method = URLRequestMethod.POST;
  3. req.data = snapshot.data;
  4. req.contentType="application/octet-stream";
  5. req.url = "snapshotuploadhandler.aspx";
  6. var loader:URLLoader = new URLLoader;
  7. loader.load(req);

在ASP.NET中读取上传了的文件是非常轻松的,代码如下:

  1. private byte[] readPostedFile()
  2. {
  3.    if (Request.ContentLength > 0)
  4.    {
  5.    byte[] buffer = new byte[Request.ContentLength];
  6.    using (BinaryReader br = new BinaryReader(Request.InputStream))
  7.    br.Read(buffer, 0, buffer.Length);
  8.    return buffer;
  9.    }
  10.    else
  11.    {
  12.    return null;
  13.    }
  14. }