aewarnick Posted March 13, 2003 Posted March 13, 2003 I found lots and lots of documentation on how to put an image into a Byte[] using MemoryStream but what about any object? It would be really nice if I could put any object into a Byte[]. Quote C#
aewarnick Posted March 20, 2003 Author Posted March 20, 2003 (edited) I have seen on the web alot of documentation on the memory stream. It has a method ToArray() to convert an object to a byte[] but no matter what I do I cannot get memory stream to have anything in the stream. Maybe something like this: public static byte[] GetByteArray(object o) { using (MemoryStream ms = new MemoryStream()) { return ms.ToArray(); } } Although it does not work. Please help, or give me assurance that putting an object into a byte[] is impossible. Edited March 20, 2003 by aewarnick Quote C#
*Gurus* divil Posted March 20, 2003 *Gurus* Posted March 20, 2003 No, you can't just put any object in to a byte array. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Volte Posted March 20, 2003 *Experts* Posted March 20, 2003 You can, however, serialize objects into XML or binary; look into the Xml.Serialization namespace to serialize to XML, and the System.Runtime.Serialization.Formatters.Binary namespace to serialize to binary. What serialization does is take all the properties of an object and store them in a file or in memory so you can store them easily (to send over the internet, store settings, etc). Quote
aewarnick Posted March 20, 2003 Author Posted March 20, 2003 I think I figured out the exception to that. I serialize the object to disk first and then deserialize it. That works. I have no idea why I would want to do that though. Quote C#
aewarnick Posted March 20, 2003 Author Posted March 20, 2003 I did not see that you posted before I did vf. I'm thinking right along your lines. I have a question about that though. When I serialize an image to disk it never shows a thumbnail like all the other pictures do. But I can open it in any picture editor just fine. What am I doing wrong? Quote C#
bpayne111 Posted March 21, 2003 Posted March 21, 2003 Actually i'm pretty sure serialization is a fast operation to perform. I use it in my Clone method of a class i created. Worked like a charm and didn't seem to eat up alot of time. I'm interested to see what the experts have to say about it. Quote i'm not lazy i'm just resting before i get tired.
aewarnick Posted March 21, 2003 Author Posted March 21, 2003 What I meant was, I have no idea why I would want to serialize and then deserialize an object just to put it into a byte array. Because the only reason I want something in a byte[] is to save it to disk with a FileStream. Serialization does seem pretty fast. I found that using method Save() to save an image to disk saves it correctly whereas serialization does not. Quote C#
Jedhi Posted March 8, 2004 Posted March 8, 2004 Did you work out any solution ? I'm sitting with the same problem. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.