c# - 从 Windows Azure 存储检索图像 (Base64String)

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

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

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

  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);

类(class):

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

最佳答案

您可以通过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();

关于c# - 从 Windows Azure 存储检索图像 (Base64String),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17403538/

相关文章:

javascript - "touchend"的 MSPointer 等效项

python - 图像转base64 : Why don't I get the same return value while calling the same function in Python?

javascript - 在本地存储中存储 .jpg 文件

windows - ExifReader.ReadJpeg 问题

c# - 为什么我必须在泛型类的静态方法调用上使用 <T>

c# - ReadAsMultipartAsync 和 MultiPartFormDataStreamProvider 出错

c# - 使用 TPL 的 Parallel.ForEach 时跟踪进度

c# - 如何将方形图像蒙版为圆角图像?

php - htaccess 删除 base 64 编码的 url 符号

c# - Restsharp 引用中的版本问题