在windows phone 8.1中将Base64String图像显示为MapIcon图像

本文关键字:图像 显示 MapIcon Base64String 中将 windows phone | 更新日期: 2023-09-27 18:29:44

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

用于将字符串转换为图像的代码

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

在windows phone 8.1中将Base64String图像显示为MapIcon图像

尝试使用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);
        }
    }
}