c# - System.Drawing.Image Parameter Not Valid -
i know has been asked numerous times... , have searched , tried can, still not sure why "parameter not valid" exception...
i have amazon ec2 instance running windows server 2012. on machine, running unity3d (unity 5). unity application sending frames (images) ec2 instance local laptop, via tcp. client running windows 10, not make difference.
to image data, following:
byte[] getscreendata() { // create texture size of screen, rgb24 format int width = screen.width; int height = screen.height; rendertexture rt = new rendertexture(width, height, 24); texture2d tex = new texture2d(width, height, textureformat.rgb24, false); camera camera = gameobject.find("main camera").getcomponent < camera > (); camera.targettexture = rt; camera.render(); rendertexture.active = rt; // read screen contents texture tex.readpixels(new rect(0, 0, width, height), 0, 0); camera.targettexture = null; rendertexture.active = null; destroy(rt); // encode texture jpg byte[] bytes = tex.encodetojpg(); destroy(tex); return bytes; }
i serialize data using flatbuffers:
public static byte[] flatserialize(this servertoclientmessage message) { var builder = new flatbufferbuilder(1); //create id var messageid = builder.createstring(message.messageid.tostring()); //start vector... //loop on each byte , add - god, there not better way? flatservertoclientmessage.startimagebytesvector(builder, message.imagebytes.length); foreach(var imagebyte in message.imagebytes) { builder.addbyte(imagebyte); } var imagebytes = builder.endvector(); //start flatservertoclientmessage , add messageid , imagebytes flatservertoclientmessage.startflatservertoclientmessage(builder); flatservertoclientmessage.addmessageid(builder, messageid); flatservertoclientmessage.addimagebytes(builder, imagebytes); //end flatservertoclientmessage , finish it... var flatmessage = flatservertoclientmessage.endflatservertoclientmessage(builder); flatservertoclientmessage.finishflatservertoclientmessagebuffer(builder, flatmessage); return builder.sizedbytearray(); }
next, send data:
public void sendraw(byte[] datatosend) { ///we must send length of message before sending actual message var sizeinfo = new byte[4]; // = bitconverter.getbytes(datatosend.length); //shift bytes sizeinfo[0] = (byte) datatosend.length; sizeinfo[1] = (byte)(datatosend.length >> 8); sizeinfo[2] = (byte)(datatosend.length >> 16); sizeinfo[3] = (byte)(datatosend.length >> 24); try { var stream = client.getstream(); //send length of data stream.write(sizeinfo, 0, 4); //send data stream.write(datatosend, 0, datatosend.length); } catch (exception ex) { debug.logexception(ex); } { //raise event tell system client has disconnected , listening must restart... } }
back on client device, listening incoming data deserializes , raises event alert system arrival of new image...
private void run() { try { // shutdownevent manualresetevent signaled // client when time close socket. while (!shutdownevent.waitone(0)) { try { if (!_stream.dataavailable) continue; //read first 4 bytes represent size of message, , convert byte array int32 var sizeinfo = new byte[4]; _stream.read(sizeinfo, 0, 4); var messagesize = bitconverter.toint32(sizeinfo, 0); //create new buffer data read var buffer = new byte[messagesize]; var read = 0; //continue reading stream until have read bytes @messagesize while (read != messagesize) { read += _stream.read(buffer, read, buffer.length - read); } var message = new servertoclientmessage().flatdeserialize(buffer); //raise data received event ondatareceived(message); } catch (ioexception ex) { // handle exception... } } } catch (exception ex) { // handle exception... } { _stream.close(); } }
to deserialize, following:
public static servertoclientmessage flatdeserialize(this servertoclientmessage message, byte[] bytes) { var bb = new bytebuffer(bytes); var flatmessage = flatservertoclientmessage.getrootasflatservertoclientmessage(bb); message.messageid = new guid(flatmessage.messageid); message.imagebytes = new byte[flatmessage.imagebyteslength]; (var = 0; < flatmessage.imagebyteslength; i++) { message.imagebytes[i] = flatmessage.getimagebytes(i); } return message; }
for clarity, here servertoclientmessage
class:
public class servertoclientmessage : eventargs { public guid messageid { get; set; } public byte[] imagebytes { get; set; } }
anyway, next, ondatareceived
event gets raised , in turn calls function convert imagebytes
array system.drawing.image. function here:
public image bytearraytoimage(byte[] bytearrayin) { // same problem! //var converter = new system.drawing.imageconverter(); // return (image)converter.convertfrom(bytearrayin); ; using(var memorystream = new memorystream(bytearrayin)) { return image.fromstream(memorystream, false, false); } }
now, image data being sent server fine , dandy... have validated it. works fine when use json, too. i've tried many ways convert byte array image, seem parameter not valid
exception. i've tried sending image in different formats jpg , png, raw pixel data.
anyone have idea?
figured out.
it turns out data backwards...due flatbuffers serialization. solution reverse order of for-loop
during serialization:
for (var = message.imagebytes.length; -->0;) { builder.addbyte(message.imagebytes[i]); }
Comments
Post a Comment