来自字节数组的图像未显示

时间:2013-09-05 22:02:56

标签: c# .net windows-phone-7 windows-phone-7.1 bitmapimage

我有一个应用程序可以拍照,将它们转换为字节数组并将它们本地保存在独立存储中。然后它将它们读出并将它们转换回BitmapImage。

但是,我似乎无法在ListBox中显示图像。我在另一个完美运行的页面中使用了相同的代码。

BitmapImage中有一个图像,但这就像我所知道的那样。无论该图像是否合法,我都不知道或不知道如何检查。

非常感谢任何想法。

查看以下代码

转换图片

 public byte[] ImageToBytes(BitmapImage img)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            WriteableBitmap btmMap = new WriteableBitmap(img.PixelWidth, img.PixelHeight);
            // write an image into the stream
            Extensions.SaveJpeg(btmMap, ms, img.PixelWidth, img.PixelHeight, 0, 100);
            return ms.ToArray();
        }
    }
    public BitmapImage BytesToImage(byte[] bytes)
    {
        BitmapImage bitmapImage = new BitmapImage();
        MemoryStream ms = new MemoryStream(bytes);
        bitmapImage.SetSource(ms);
        return bitmapImage;
    }

带图像的课程

public class NewItem
{
    ObservableCollection<BitmapImage> images = new ObservableCollection<BitmapImage>();
    [DataMember]
    public ObservableCollection<BitmapImage> Images
    {
        get { return images; }
        set { images = value; }
    }
    [DataMember]
    public string Notes { get; set; }
    [DataMember]
    public string ItemID { get; set; }
}

保存到存储空间

public static void AddOrUpdateUnsavedItems(NewItem _item)
    {
        var store = IsolatedStorageFile.GetUserStoreForApplication();
        List<NewItem> allunsaveditems = new List<NewItem>();
        if (store.FileExists("unsaveditem"))
        {

            allunsaveditems.Add(_item);
            allunsaveditems.AddRange(LoadUnsavedItemsFromIsolatedStorage());
            store.DeleteFile("unsaveditem");
        }
        UnsavedRegisters.Clear();
        foreach (NewItem ni in allunsaveditems)
        {
            StoredItem newUnsavedItem = new StoredItem();
            newUnsavedItem.ItemID = ni.ItemID;
            newUnsavedItem.Notes = ni.Notes;
            foreach (BitmapImage bmp in ni.Images)
            {
                newUnsavedItem.ImageBytes.Add(newUnsavedItem.ImageToBytes(bmp));
            }
            UnsavedRegisters.Add(newUnsavedItem);
        }
        using (var stream = new IsolatedStorageFileStream("unsaveditem", FileMode.OpenOrCreate, FileAccess.Write, store))
        {                
            DataContractSerializer dcs = new DataContractSerializer(typeof(List<StoredItem>));
            dcs.WriteObject(stream, UnsavedRegisters);
        }

    }

从存储中加载

public static List<NewItem> LoadUnsavedItemsFromIsolatedStorage()
    {
        List<NewItem> unsavedItems = new List<NewItem>();
        try
        {
            var store = IsolatedStorageFile.GetUserStoreForApplication();
            if (store.FileExists("unsaveditem"))
            {
                using (var stream = new IsolatedStorageFileStream("unsaveditem", FileMode.OpenOrCreate, FileAccess.Read, store))
                {
                    if (stream.Length > 0)
                    {
                        DataContractSerializer dcs = new DataContractSerializer(typeof(List<StoredItem>));
                        List<StoredItem> storedItems = dcs.ReadObject(stream) as List<StoredItem>;
                        foreach (StoredItem si in storedItems)
                        {
                            NewItem ni = new NewItem();
                            ni.ItemID = si.ItemID;
                            ni.Notes = si.Notes;
                            foreach (byte[] imageBytes in si.ImageBytes)
                            {
                                ni.Images.Add(si.BytesToImage(imageBytes));
                            }
                            unsavedItems.Add(ni);
                        }
                    }
                }
            }
        }
        catch (Exception)
        {
            //MessageBox.Show("and error happened getting the unsaved items");
            // handle exception
            return null;
        }
        return unsavedItems;
    }

2 个答案:

答案 0 :(得分:0)

这应该不是问题,我有一个BitmapSource工作,我相信BitmapImage继承,尝试下面的列表框中的代码

 <Border Height="200" Width="200">
                            <Border.Background>
                                <ImageBrush ImageSource="{Binding ItemImage}" />
                            </Border.Background>
                        </Border>

ItemImage是保存BitmapSource图像的属性。

答案 1 :(得分:0)

我不知道发生了什么变化,但它开始起作用了。