在Windows Phone 8.1中将Base64String图像显示为MapIcon图像

时间:2016-01-10 04:56:21

标签: c# geolocation windows-phone-8.1

我将图像转换为base64字符串并保存到SQLite数据库。我想在地图中显示该图像。

用于将String转换为图像的代码

    public static BitmapImage Base64StringToBitmap(string source)
    {
        var ims = new InMemoryRandomAccessStream();
        var bytes = Convert.FromBase64String(source);
        var dataWriter = new DataWriter(ims);
        dataWriter.WriteBytes(bytes);
        dataWriter.StoreAsync();
        ims.Seek(0);
        var img = new BitmapImage();
        img.SetSource(ims);
        return img;
    }

如何在地图中显示此图像。显示图钉

    private void loadPushpin(object sender, RoutedEventArgs e)
    {
        using (var dbConn = new SQLiteConnection(App.DB_PATH))
        {
            var query = dbConn.Table<Contacts>();
            var result = query.ToList();
            foreach (var item in result)
            {
                 Contacts obj = new Contacts();
                 MapIcon MapIcon1 = new MapIcon();
                 MapIcon1.Image = RandomAccessStreamReference.CreateFromUri(new Uri(""));
                 MapIcon1.Location = new Geopoint(new BasicGeoposition()
                 {
                     Latitude = Convert.ToDouble(item.Latitude),
                     Longitude = Convert.ToDouble(item.Longitude)
                 });
                   MapIcon1.NormalizedAnchorPoint = new Point(0.5, 1.0);
                   MapIcon1.Title = item.Name;
                 MyLocationMap.MapElements.Add(MapIcon1);
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

尝试使用CreateFromStream并传递InMemboryRandomeAccessStream而不是CreateFromUri。以下是代码

  var ims = new InMemoryRandomAccessStream();
        var bytes = Convert.FromBase64String(source);
        var dataWriter = new DataWriter(ims);
        dataWriter.WriteBytes(bytes);
        dataWriter.StoreAsync();
        ims.Seek(0);
        var img = new BitmapImage();
        img.SetSource(ims);
        return img;

        MapIcon MapIcon1 = new MapIcon();
        MapIcon1.Image = RandomAccessStreamReference.CreateFromStream(ims);;

<强>更新

   private async void loadPushpin(object sender, RoutedEventArgs e)
{
    using (var dbConn = new SQLiteConnection(App.DB_PATH))
    {
        var query = dbConn.Table<Contacts>();
        var result = query.ToList();
        foreach (var item in result)
        {
             Contacts obj = new Contacts();
             MapIcon1.Location = new Geopoint(new BasicGeoposition()
             {
                 Latitude = Convert.ToDouble(item.Latitude),
                 Longitude = Convert.ToDouble(item.Longitude)
             });

            var ims = new InMemoryRandomAccessStream();
            var bytes = Convert.FromBase64String(item.ImageString);//set your image base 64 string here...
            var dataWriter = new DataWriter(ims);
            dataWriter.WriteBytes(bytes);
            await dataWriter.StoreAsync();
            ims.Seek(0);

           MapIcon MapIcon1 = new MapIcon();
           MapIcon1.Image = RandomAccessStreamReference.CreateFromStream(ims);


               MapIcon1.NormalizedAnchorPoint = new Point(0.5, 1.0);
               MapIcon1.Title = item.Name;
             MyLocationMap.MapElements.Add(MapIcon1);
        }

    }
}