从Windows Azure存储中检索图像(Base64String)

时间:2013-07-01 11:35:42

标签: c# windows-phone-8 base64 azure-storage azure-mobile-services

如何检索我在Windows Azure中存储为Base64String的图像?我知道如何在Windows Azure中将图像保存为Base64String,但我不知道如何检索它。

将数据保存为Windows Azure存储为Base64String:

  private MemoryStream str;

  str = new MemoryStream();

  WriteableBitmap wb;

  wb = new WriteableBitmap(bmp);

  wb.SaveJpeg(str, bmp.PixelWidth, bmp.PixelHeight, 0, 100);

  Item item = new Item { ImageString = System.Convert.ToBase64String(str.ToArray()) };

  App.MobileService.GetTable<Item>().InsertAsync(item);

类别:

 public class Item
{
    public int Id { get; set; }
    public string ImageString { get; set; }
}

1 个答案:

答案 0 :(得分:2)

您可以按ID查询图片(将在调用InsertAsync后返回):

private void RetrieveImage(int id) {
    var item = await App.MobileService.GetTable<Item>().LookupAsync(id);
    byte[] imageBytes = Convert.FromBase64String(item.ImageString);
}

或者检索所有图像:

private void RetrieveAllImages() {
    var images = await App.MobileService
        .GetTable<Item>()
        .Select(i => Convert.FromBase64String(i.ImageString))
        .ToListAsync();
}

或使用任意属性(而不是id)进行查询 - 假设Item类具有名为“Name”的属性:

var items = await App.MobileService.GetTable<Item>()
    .Where(it => it.Name == "MyImage")
    .ToEnumerableAsync();
var item = item.FirstOrDefault();