We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How can I load a external image to Bitmapdata object in flash to manipulate the pixels correctly?

prsr4000
prsr4000 asked
on
Medium Priority
726 Views
Last Modified: 2013-11-11
Anyone know how to change the following code to make it working, so that I can upload an external image to the bitmap data object in flash. Following code is in the first frame on the root.

Thanks in advance.
Stage.scaleMode = 'noScale';
 
/*
This examples shows how bitmaps can be used to erase or redraw
another bitmap using its alpha channel.
*/
 
var tool; // used to determine the current tool
var toolsize = 40; // size used for tools (width = height)
var tooloffset = toolsize/2; // offset for cursor placement in using tool
var basepoint = new flash.geom.Point(0, 0); // 0,0 point
 
// two bitmaps are used with the image being manipulated
// the first, origbmp contains the original image, unaltered
// the second, drawbmp is the working image seen and
// altered on the screen
 
_root.pic_mc.loadMovie("test.jpg",1);
_root.pic_mc._visible = false;
 
//var origbmp = new flash.display.BitmapData(200, 200, true, 0);
//origbmp.draw(pic_mc);
var origbmp = flash.display.BitmapData.loadBitmap("dragon_bmp");
var drawbmp = new flash.display.BitmapData(origbmp.width, origbmp.height, true, 0);
// drawbmp starts with a copy of the original
drawbmp.draw(origbmp);
 
// two bitmaps are used for the tools, one for erasing
// and one for redrawing, both same size and settings
var erasebmp = new flash.display.BitmapData(toolsize, toolsize, true, 0);
var redrawbmp = erasebmp.clone();
 
// create a movie clip, image_mc, to hold the
// image being erased and redrawn
this.createEmptyMovieClip("image_mc", 1);
// position the image to the left
image_mc._x = 84;
 
// attach the drawing bitmap data object to image_mc
image_mc.attachBitmap(drawbmp, 1);
 
 
// -------------------------------------
 
// TOOLS
 
// select erase tool
erase_btn.onPress = function(){
	
	// tell button movie clips to navigate to appropriate state
	this.gotoAndStop(2);
	redraw_btn.gotoAndStop(1);
	
	// assign tool to equal "erase"
	// this is checked when drawing starts
	tool = "erase";
}
 
// select redraw tool
redraw_btn.onPress = function(){
	
	// tell button movie clips to navigate to appropriate state
	this.gotoAndStop(2);
	erase_btn.gotoAndStop(1);
	
	// assign tool to equal "redraw"
	// this is checked when drawing starts
	tool = "redraw";
}
 
// -------------------------------------
 
// BRUSHES
 
// select circle brush
circle_btn.onPress = function(){
	
	// tell button movie clips to navigate to appropriate state
	this.gotoAndStop(2);
	line_btn.gotoAndStop(1);
	
	// call setToolShape with circle_mc to set
	// the tool(s) shape(s) to a circle
	setToolShape(circle_mc);
}
// select line brush
line_btn.onPress = function(){
	
	// tell button movie clips to navigate to appropriate state
	this.gotoAndStop(2);
	circle_btn.gotoAndStop(1);
	
	// call setToolShape with circle_mc to set
	// the tool(s) shape(s) to a line
	setToolShape(line_mc);
}
 
function setToolShape(shape_mc){
	
	// erasebmp needs to be transparent where it is supposed to
	// be erasing drawbmp.  First fill it with solid white
	erasebmp.fillRect(erasebmp.rectangle, 0xFFFFFFFF);
	// then draw the shape (black) into the bitmap
	erasebmp.draw(shape_mc);
	// copyChannel is used to convert the shape of the tool
	// just added into erasebmp into its alpha channel
	erasebmp.copyChannel(erasebmp, erasebmp.rectangle, basepoint, 1, 8);
 
	// redrawbmp needs to be solid where it redraws drawbmp
	// fill redrawbmp with invisible black (0x00000000) 
	redrawbmp.fillRect(redrawbmp.rectangle, 0);
	// add the shape (solid color)
	redrawbmp.draw(shape_mc);
}
 
// -------------------------------------
 
// WORKING WITH IMAGE
 
// pressing image to erase or redraw
image_mc.onPress = function(){
	
	// depending on selected tool, assign onMouseMove event handler
	if (tool == "erase") this.onMouseMove = useEraserMouseMove;
	else if (tool == "redraw") this.onMouseMove = useRedrawMouseMove;
	
	// initiate onMouseMove handler to work with onPress
	this.onMouseMove();
}
 
// releasing after erase or redraw
image_mc.onRelease = image_mc.onReleaseOutside = function(){
	
	// remove onMouseMove handler
	delete this.onMouseMove;
}
 
// onMouseMove event handler for erasing
function useEraserMouseMove(){
	
	// devise an offset based on the mouse's
	// position in image_mc and tooloffset
	var offset = new flash.geom.Point(image_mc._xmouse - tooloffset, image_mc._ymouse - tooloffset);
	
	// devise a drawing rectangle the size of the
	// the drawing tools at the offset's location
	var drawRect = new flash.geom.Rectangle(offset.x, offset.y, toolsize, toolsize);
	
	// next, copy pixels from the drawbmp back onto itself
	// (makes for no change) but use erasebmp as the alphaBitmap
	// this will add the alpha channel information from erasebmp
	// into drawbmp erasing pixels from drawbmp in the shape of erasebmp
	drawbmp.copyPixels(drawbmp, drawRect, offset, erasebmp, basepoint, false);
	
	// update after each mouse movement
	updateAfterEvent();
}
 
// onMouseMove event handler for redrawing
function useRedrawMouseMove(){
	
	// devise an offset based on the mouse's
	// position in image_mc and tooloffset
	var offset = new flash.geom.Point(image_mc._xmouse - tooloffset, image_mc._ymouse - tooloffset);
	
	// devise a drawing rectangle the size of the
	// the drawing tools at the offset's location
	var drawRect = new flash.geom.Rectangle(offset.x, offset.y, toolsize, toolsize);
	
	// next, copy pixels from the original bitmap back
	// onto drawbmp to update the image. redrawbmp is
	// used as the alphaBitmap to give the tool shape
	drawbmp.copyPixels(origbmp, drawRect, offset, redrawbmp, basepoint, true);
	
	// update after each mouse movement
	updateAfterEvent();
}
 
// -------------------------------------
 
// initialize
erase_btn.onPress(); // start with erase tool
circle_btn.onPress(); // start with circle brush

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
That was partially helpful. I tried loading the image in a movie clip and assigned the bitmapdata to the new obj. But it was not working as the loading was done instant.

So I added a time delay in loading with a bytes loaded checking as two different frames and it worked.

Thank you for your time.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.