【发布时间】:2013-07-29 06:42:08
【问题描述】:
我正在尝试将位图图像转换为 字节数组。我使用 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 文件。当我提取该文件时,有一些图像,但问题是当我双击图像时,我看不到该图像。我认为问题在于读取图像的字节。我不明白怎么了?我的代码正确吗?
【问题讨论】:
-
你能用
ms.ToArray()代替ms.GetBuffer()吗? -
@KooKiz 感谢您的回复。我试过了,但它不起作用
-
附带说明,您不必使用字节数组。搜索完后直接调用
ms.CopyTo(stream);。它不会解决您的问题,但它会使您的代码更易于阅读并节省一些 RAM。并且搜索应该在 ms 上调用,而不是在流上。
标签: c# windows-phone-7 windows-phone-8 isolatedstorage