【问题标题】:Show Base64String image as MapIcon image in windows phone 8.1在 windows phone 8.1 中将 Base64String 图像显示为 MapIcon 图像
【发布时间】:2016-01-10 04:56:20
【问题描述】:

我将图像转换为 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);
            }

        }
    }

【问题讨论】:

    标签: c# geolocation windows-phone-8.1


    【解决方案1】:

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

    【讨论】:

    • 你能解释一下如何做到这一点。我在问题中更新了我的代码
    • 感谢它完美运行。一件小事,我怎样才能改变图像属性(图像的大小)。
    • 您无法更改 mapIcon 图像的大小,因为它是 Stream。在这种情况下,在您的数据库中使用更小或更大的图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多