简体   繁体   English

将二进制数据保存到C#/ silverlight 3中的图像文件中

[英]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. 我在Byte数组中有一个二进制数据。 Does anybody know how to save this into any image file (png or jpg) in silverlight3 or c#? 有谁知道如何将其保存到silverlight3或c#中的任何图像文件(png或jpg)中?

Thanks 谢谢

To save a byte array (verbatim) to a file : 要将字节数组(verbatim)保存到文件:

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/ 无论如何,请查看ImageTools库: 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. 由于听起来数据已经是您想要的格式,因此问题看起来更像是“如何将流的内容保存到文件中”,Moe正确回答了此问题。 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 :) 如果需要将所有内容存储在内存中,则可以File.Create ,然后从一个流复制到另一个流(如果在.Net 4中,则可以使用新的CopyTo方法为您完成此操作-如果降级,则可以手动执行或者只是使用扩展方法来获得相同的行为:)

If you happen to need to change the format of the image, you can use System.Drawing.Image for that. 如果您碰巧需要更改图像的格式,则可以使用System.Drawing.Image。 For instance, Image.FromStream and then the Save method . 例如, Image.FromStream然后是Save方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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