Using .NET/C# to generate bitmap from Flash 8
*update*
2006-06-20. I added a progress bar to make it more clear what is happening.
I have been searching for cool ways to use flash for creating gallery functions since it's both cool and easy to make great functions. And now when we can generate files on disk it is really usefull!
There are many examples around for this but I did not really find suitable code for me, using .Net/C#, so after messing around a bit i put together a nice solution using a .NET Web service. Please be patient, it will take a few seconds to generate the pixel array and send it to the web service.
Check it out:
The generated image will show in a new window.
Flash
function Generate(obj:MovieClip):Void
{
var pixelarr:Array = new Array()
var bmp:BitmapData = new BitmapData(obj._width, obj._height,false,0xffffff);
var myMatrix:Matrix = new Matrix();
//No positioning
myMatrix.translate(0, 0);
bmp.draw(obj.Picture,myMatrix);
var w:Number = bmp.width;
var h:Number = bmp.height;
for(var a=0; a ‹ h; a++)
{
for(var b=0; b‹w; b++)
{
pixelarr.push(bmp.getPixel32(b, a));
}
}
var wsdlURI = "http://www.2cool2care.com/flash/Service.asmx";
var myWebService:WebService = new mx.services.WebService(wsdlURI+"?WSDL");
var callback1:PendingCall = myWebService.SaveImage(pixelarr.toString(),h,w);
callback1.onResult = function(result)
{
//result is the file name
if(result)
{
getURL("http://www.2cool2care.com/flash/"+result,"_blank");
trace(result);
}
}
callback1.onFault = function(fault)
{
trace("Err: "+fault.faultstring)
}
}
C#
[WebMethod]
public string SaveImage(string Pixels,int Height, int Width)
{
//Create pixel array
String[] PxArr = Pixels.Split(',');
Bitmap MyImage = new Bitmap(Width, Height);
Graphics Gfx = Graphics.FromImage(MyImage);
Gfx.DrawRectangle(new Pen(Color.Red), new Rectangle(0, 0, Width, Height));
int k = 0;
//Loop and paint pixels
for (int i = 0; i ‹ Height; i++)
{
for (int j = 0; j ‹ Width; j++)
{
MyImage.SetPixel(j, i, Color.FromArgb(Convert.ToInt32(PxArr[k])));
k++;
}
}
//Randomize filename and save
Random RandomClass = new Random();
String Filename = RandomClass.Next().ToString() + ".png";
MyImage.Save(Server.MapPath(Filename), System.Drawing.Imaging.ImageFormat.Png);
return Filename;
}
I Hope someone finds this useful!
Take care,
Rob
Tuesday, February 14, 2006
Subscribe to:
Post Comments (Atom)
4 comments:
sweet
This is something I've been looking to do, thanks for this!
i wonder if there is an easier way to do this now? do you know? (i found your page from actionscript.org)
i'm working on a project right now similar to this. it's an interactive app where users can design their product and i'd like to add some functionality for them to be able to save it or print it or email it. could i modify this to accomplish that? thanks!
@abbie-road:
You can not in the current flash versions save directly to the client, but you must post the data to a server and then download it as i this example. Printing is fine, and i'm not sure about e-mail. I don't think you can attach a file though flash only.
Post a Comment