I have an application that asks users to upload a photo via flash. Flash then turns this photo into a byte array. Here is that code:
var rect:Rectangle=new Rectangle(0,0,imageWidth,imageHeight);
// create BitmapData
var bmd:BitmapData=new BitmapData(imageWidth,imageHeight,true,0);
bmd.draw(myMovieClip);// get the image out of the MovieClip
// create byte array
var binaryData:ByteArray = new ByteArray();
binaryData.position=0;// start at the beginning
binaryData.writeBytes(bmd.getPixels(rect));
After the byte array is created, it is base64 encoded and posted to an IIS server that houses an .ashx handler. In the handlers ProcessRequest method the following code is used to write the data out to a file:
try
{
// Convert from base64string to binary array and save
byte[] contents = Convert.FromBase64String(encodedData); //encodedData is the Base64 encoded byte array from flash
File.WriteAllBytes(fullPath, (byte[])contents);
context.Response.Write("result=1");
}
catch (Exception ex)
{
context.Response.Write("errMsg=" + ex.Message + "&result=0");
}
So far so good. No problems up to this point.
The problem occurs after retrieving the byte data that is stored in the file and attempting to recreate the image on the server side in C#.
When I try the following:
try
{
var fileBytes = File.ReadAllBytes(path);
Bitmap bmp;
using (var ms = new MemoryStream(fileBytes))
{
ms.Position = 0;
using (var i = Image.FromStream(ms, false, true))
{
bmp = new Bitmap(i);
}
}
}
catch (Exception ex)
{
//error: ex.Message is always “the parameter is not valid”
}
The program always throws on this line with the message “the parameter is not valid”
using (var i = Image.FromStream(ms, false, true))
Any advice is greatly appreciated.
Image.FromFileinstead to avoid writing so much code. But first: can you open the saved file in an image viewer (e.g. Paint)?