- Home /
C# convert Bytearray to System.Draw.Image
Hello,
I need to convert a Byte array back into an image. If I do that in the Unity IDE it is working but if I do it after I compiled it in windows it is not working anymore. I am converting the Byte array into a Stream and try to convert the Stream into an image afterwards.
Stream tempstream;
System.Drawing.Image tempimage = null;
try{
Byte[] ba;
ba = (Byte[]) reader["FlagPng"];
Debug.Log ("ByteArray: "+ba.ToString ());
tempstream = new MemoryStream(ba);
Debug.Log ("tempstream: "+tempstream.ToString ());
tempimage = Image.FromStream(tempstream);
}
catch(Exception ex){
Debug.LogError ("Errormessage 100: \n "+ex.Message );
Debug.LogError ("Errorsource 100: \n "+ex.Source );
Debug.LogError ("Errorstacktrace 100: \n "+ex.StackTrace);
Debug.LogError ("Errortargetsite 100: \n "+ex.TargetSite);
}
In my output_log is written: ByteArray: System.Byte[]
tempstream: System.IO.MemoryStream
Errormessage 100: Could not load type 'System.IO.InternalBufferOverflowException' from assembly 'System.Drawing'.
Errorsource 100: System.Drawing
Errorstacktrace 100: at System.Drawing.Image.InitFromStream (System.IO.Stream stream) [0x00000] in :0
at System.Drawing.Image.LoadFromStream (System.IO.Stream stream, Boolean keepAlive) [0x00000] in :0
at System.Drawing.Image.FromStream (System.IO.Stream stream) [0x00000] in :0
at SQLDatabase.ReadTblLanguages () [0x00000] in :0
Errortargetsite 100: IntPtr InitFromStream(System.IO.Stream)
I can tell that the problem should be the line: tempimage = Image.FromStream(tempstream);
All dlls I am using are mono dlls from the library directory of Unity.
Can anyone tell me how I can convert a stream into an image in Unity?
Thank you in advance.
Answer by Julien-Lynge · Jan 21, 2013 at 07:52 PM
I'm going to refer you to other questions - the problem is with System.Drawing:
http://answers.unity3d.com/questions/26576/why-is-the-systemdrawing-namespace-not-supported-i.html
http://forum.unity3d.com/threads/13191-System.Drawing-Question
Thank you for your hint. I worked it out another way. Unity offers the function Texture2D.LoadImage which can handle a Byte array.
Yep, as long as the bytes correspond with a jpg or png you're fine there.
Answer by unity_Gf9sAXj5a0VNOg · Oct 30, 2017 at 02:48 PM
demonstrated how to convert byte array to image and save in c# and upload image on server in aspnet using c# with an example. Find the c# source code below to convert byte array to image c# here
Your answer
Follow this Question
Related Questions
String Parsing 1 Answer
Using an array instead 2 Answers
Help converting this to C# - a few issues 3 Answers
Convert to array. 1 Answer
Get JSON array object string value 2 Answers