Take a screenshot in Flex and send it to ASP.NET
In Adobe Flex 3, you can get a bitmap image of any control by using this code (you’ll need to import "mx.graphics.ImageSnapshot"):
var snapshot:ImageSnapshot = ImageSnapshot.captureImage(backgroundCanvas);
By default, it uses PNG encoding.
If you want to then send this image to the server, use this code:
var req:URLRequest = new URLRequest(); req.method = URLRequestMethod.POST; req.data = snapshot.data; req.contentType="application/octet-stream"; req.url = "snapshotuploadhandler.aspx"; var loader:URLLoader = new URLLoader; loader.load(req);
Reading the uploaded file is easy using ASP.NET:
private byte[] readPostedFile()
{
if (Request.ContentLength > 0)
{
byte[] buffer = new byte[Request.ContentLength];
using (BinaryReader br = new BinaryReader(Request.InputStream))
br.Read(buffer, 0, buffer.Length);
return buffer;
}
else
{
return null;
}
}

Ria Flex said,
Wrote on April 22, 2008 @ 4:11 pm
Nice example but I was wondering why you used URLLoader instead of HttpService?
SuperJason said,
Wrote on April 22, 2008 @ 4:21 pm
I didn’t really have a preference. I’m kind of assuming that the URLLoader is a little more lightweight. (but you know what they say about people that assume)
Is there an advantage to using HttpService?
Adam said,
Wrote on April 24, 2008 @ 3:57 pm
I’m so lucky you blogged this just a couple days ago – it was exactly what I’m looking for. Thanks!
Roohullah said,
Wrote on April 26, 2008 @ 2:06 am
this is very cool sample of flex ability in bitmap tools.
thank you
oh, i agree SuperJason, because in this case we should use this class (simple and lighter)
in the adobe live docs, advise to all user if we want use the actionscript only, the URLLoader is better.
Daily del.icio.us for April 26th through April 27th — Vinny Carpenter’s blog said,
Wrote on April 27, 2008 @ 7:06 pm
[...] Take a screenshot in Flex and send it to ASP.NET | YTechie.com – In Adobe Flex 3, you can get a bitmap image of any control by using this code (you’ll need to import "mx.graphics.ImageSnapshot"): [...]
links for 2008-04-29 « Tathata - d’ Observer said,
Wrote on April 28, 2008 @ 8:53 pm
[...] Take a screenshot in Flex and send it to ASP.NET (tags: flex screenshot customer service) [...]
Andrew Hopkins said,
Wrote on July 15, 2008 @ 5:35 am
Any chance of showing the full C# code, or another example in VB.net?
Thanks
A
Xun said,
Wrote on October 27, 2008 @ 5:02 pm
very nice blog
Greg said,
Wrote on July 8, 2009 @ 9:04 am
This is excellent! Im wondering what adjustments I might need to make here to send a file instead of a screenshot. Im using AIR and I have the filestream. Is it best to somehow convert this to a byte[] on the client side and use the same server code, or send the filestream across (as a string?) and modify the server code to suit.
Any thoughts here?
Khurram said,
Wrote on October 6, 2009 @ 6:09 am
Nice Example
Cna I use this to take a snapshot of user desktop. Like the print screen button do it?
ChessMaster said,
Wrote on February 25, 2010 @ 3:51 pm
Hi!
I am trying to do the same with httpService…
Could you please explain how did you pass the screenshot to your request. Do not see the assignment
snapshot variable to request….
ChessMaster said,
Wrote on February 25, 2010 @ 3:52 pm
Ah sorry…. Just realized req.data = snapshot.data;
Please ignore my prev comment
Defgod said,
Wrote on April 28, 2010 @ 1:10 pm
Hello folk’s,
I have been assigned a task which is basically this, however i am having issues implementing this solution.
Sending the snapshot.data via httpservice is working (i can use Request.InputStream.Length in my asp.net page to get the length, and view the properties of the object to be sent in Flex).
What i was looking for was some assistance dealing with the request in asp.net. To be clear, i dont need to save the image, just display it in a image control in asp. I appreciate any assistance i am give. And btw, i am relatively new at this, so please excuse my ignorance. Thanks in advance.