C# 将bitmapimage转换为字节数组
我正在尝试将位图图像转换为字节数组。我使用MediaLibrary类选择了所有图像,并将其添加到位图图像列表中。这是我的密码C# 将bitmapimage转换为字节数组,c#,windows-phone-7,windows-phone-8,isolatedstorage,C#,Windows Phone 7,Windows Phone 8,Isolatedstorage,我正在尝试将位图图像转换为字节数组。我使用MediaLibrary类选择了所有图像,并将其添加到位图图像列表中。这是我的密码 using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { if (!store.DirectoryExists("ImagesZipFolder")) { sto
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;
}
}
使用(IsolatedStorageFile store=IsolatedStorageFile.GetUserStoreForApplication())
{
如果(!store.DirectoryExists(“ImagesZipFolder”))
{
store.CreateDirectory(“ImagesZipFolder”);
for(int i=0;i
基本上,我正在尝试做的是,从设备中选择所有图像或照片,并创建该图像的zip文件。我成功地创建了图像的zip文件。当我提取那个文件时,有一些图像,但问题是当我双击图像时,我看不到那个图像。我认为问题在于读取图像的字节。我不明白怎么了?我的代码正确吗?也许你可以试试下面的方法。我知道这段代码维护图像,所以如果您没有使用这段代码,您可能会遇到另一个问题
// Convert the new image to a byte[]
ImageConverter converter = new ImageConverter();
byte[] newBA = (byte[])converter.ConvertTo(newImage, typeof(byte[]));
ImageConverter属于System.Drawing命名空间
更新:
您应该能够使用它代替系统。我建议的绘图类型。无需将可写位图保存到MemoryStream,然后将其复制到IsolatedStorageFileStream。只需将位图直接保存到IsolatedStorageFileStream
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"ImagesZipFolder\" + imgname[i], System.IO.FileMode.CreateNew, store))
{
WriteableBitmap wBitmap = new WriteableBitmap(ImgCollection[i]);
wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
}
这将允许您节省内存。如果你真的想节省内存,你可以重用WriteableBitmap。你能用
ms.ToArray()
代替ms.GetBuffer()
?@KooKiz谢谢你的回复。我已经试过了,但它不起作用。作为旁注,你不必使用字节数组。搜索后,直接调用ms.CopyTo(stream)代码>。它不会解决您的问题,但会使您的代码更易于阅读并节省一点RAM。搜索应该是呼叫ms,而不是流。谢谢回复。我认为系统不支持windows phoneAh,我深表歉意。这里的答案有用吗?它给我一个错误“ConvertTo未在基本类型转换器中实现”。我添加了以下代码ImageSourceConverter=new ImageSourceConverter();byte[]bytes=(byte[])ConvertTo(ImgCollection[i],typeof(byte[]);其中ImgCollection是“列表”的目标