c# - 将位图图像转换为字节数组

标签 c# windows-phone-7 windows-phone-8 isolatedstorage

我正在尝试将位图图像转换为字节数组。我已经使用 MediaLibrary 类选择了所有图像并将其添加到位图图像列表中。这是我的代码

using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!store.DirectoryExists("ImagesZipFolder"))
            {
                store.CreateDirectory("ImagesZipFolder");
                for (int i = 0; i < imgname.Count(); i++)
                {
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"ImagesZipFolder\" + imgname[i], System.IO.FileMode.CreateNew, store))
                    {
                            byte[] bytes = null;
                            using (MemoryStream ms = new MemoryStream())
                            {
                                WriteableBitmap wBitmap = new WriteableBitmap(ImgCollection[i]);
                                wBitmap.SaveJpeg(ms, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
                                stream.Seek(0, SeekOrigin.Begin);
                                bytes = ms.GetBuffer();
                                stream.Write(bytes, 0, bytes.Length);
                            }
                    //    byte[] bytes = Encoding.UTF8.GetBytes(imgname[i]);//new byte[ImgCollection[i].PixelWidth * ImgCollection[i].PixelHeight * 4];                           
                    //    stream.Write(bytes, 0, bytes.Length);
                    }
                }
            }
            else {
                directory = true;
            }
          }

基本上我想做的是,从设备中选择所有图像或照片并创建这些图像的 zip 文件。我成功地创建了图像的 zip 文件。当我提取该文件时,有一些图像,但问题是当我双击图像时,我看不到该图像。我认为问题在于读取图像的字节。我不明白怎么了?我的代码是否正确?

最佳答案

也许你可以试试下面的方法。我知道这段代码会维护图像,所以如果你没有成功使用它,你可能会遇到不同的问题。

    // Convert the new image to a byte[]
    ImageConverter converter = new ImageConverter();
    byte[] newBA = (byte[])converter.ConvertTo(newImage, typeof(byte[]));

ImageConverter 属于 System.Drawing 命名空间。


更新:

http://msdn.microsoft.com/en-GB/library/system.windows.media.imagesourceconverter.convertto.aspx

您应该可以使用它来代替我建议的 System.Drawing 类型。

关于c# - 将位图图像转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17924651/

相关文章:

c# - 我无法将 DataGrid 绑定(bind)到 ObservableCollection

c# - c#字符串有长度限制吗

c# - 如何从 PhotoChooserTask 保存图片

c# - InputPane.Showing/Hiding 从未调用过

c# - 通用 C# 复制构造函数

c# - 抛出 Win32Exception

c# - 异步替代

windows-phone-7 - Windows Phone 7 的增强现实浏览器?

c++ - Windows Phone 中没有 FileIO?

c# - 如何将 xDocument 自动转换为类