-
Flex 3 Saving a Snapshot
Posted on April 20th, 2009 No commentsI needed a way to not only take a snapshot of my Flex graph, but also the code to write the image to the webserver. I found multiple sites that gave me parts of the solution. Here is an example of all the bits put together.
1. Flex App with private or public function (depending on how you decide to access the function.)
2. Server: Rails and rMagickFlex 3 Function
private function takeSnapshot(component:UIComponent,url:String):void
{
var snapshot:ImageSnapshot = ImageSnapshot.captureImage(component, 300, new JPEGEncoder(100));
var imageGrab:ByteArray = snapshot.data as ByteArray;
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest(url);
request.requestHeaders.push(header);
request.method = URLRequestMethod.POST;
request.data = imageGrab;
var loader:URLLoader = new URLLoader();
loader.load(request);
}
Note: You’ll need to import these classes for access to the ImageSnapShot, and UIComponent types
import mx.core.UIComponent;
import mx.graphics.ImageSnapshot;
import mx.graphics.codec.*;
Rails Code:
begin
image = Magick::Image.from_blob(request.body.read).first
image.write("#{RAILS_ROOT}/public/images/test.jpg")
render :layout => false
rescue Magick::ImageMagickError
logger.error "\n\n--------------RM ERROR---------------\n\n"
logger.error "ERROR: " + $! + "\n"
logger.error "#{$0}: ImageMagickError - #{$!}"
logger.error "\n\n--------------RM ERROR----------------\n\n"
end
This solution worked great for me.
Hope it can help someone else out.
Leave a reply


