简体   繁体   中英

Save binarydata into a image file in c# / silverlight 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

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

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

Is that what you are after ?

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM