views:

330

answers:

3
   byte[] binaryData = new Byte[pngStream.Length];
   long bytesRead = pngStream.Read(binaryData, 0, (int)pngStream.Length);

   string base64String = System.Convert.ToBase64String(binaryData,
                                                          0,
                                                          binaryData.Length);

I have a binary data in Byte array. Does anybody know how to save this into any image file (png or jpg) in silverlight3 or c#?

Thanks

A: 

What format is your pixel data is in?

In any case, take a look at the ImageTools library: http://imagetools.codeplex.com/

Save yourself from reinventing the wheel

Gautam
A: 

To save a byte array (verbatim) to a file :

System.IO.File.WriteAllBytes("c:\\YourFile.png", binaryData);

Is that what you are after ?

Moe Sisko
A: 

Since it sounds like the data is already in the format you want, the question seems more like "how to I save the contents of a stream to a file", which Moe answered correctly. If storing it all in memory were a concern, you could File.Create and then copy from one stream to another (if in .Net 4, you can use the new CopyTo method to do it for you - if downlevel you can do it manually or just use an extension method to get the same behavior :)

If you happen to need to change the format of the image, you can use System.Drawing.Image for that. For instance, Image.FromStream and then the Save method.

James Manning