0

我有一个应用程序可以拍摄照片,将它们转换为字节数组并将它们本地保存在独立存储中。然后它将它们读出并将它们转换回 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;
    }
4

2 回答 2

0

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

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

ItemImage 是保存您的 BitmapSource 图像的属性。

于 2013-09-06T08:38:02.093 回答
0

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

于 2013-09-16T00:03:27.547 回答