为什么我的位图图像不显示?

时间:2014-10-24 17:14:26

标签: c# winrt-xaml bing-maps bitmapimage

Ad [a,o] pting Brundritt的例子here,他在回复my earlier BingMaps question时引用了这个例子,我已经能够在“信息框”中显示某些数据,如下所示:

enter image description here

...但是我没有得到BitmapImage,它是我打算显示的数据的一部分。

我的问题是为什么图像没有显示,我需要做什么才能显示它们?

这不是数据 - 我已将缩略图保存到SQLite数据库,在那里它存储为一个字节数组,但可以看作是SQLiteToolbox中的图像:

SQLiteToolbox

正如您所看到的,String数据显示正常,但不是图像。以下是我检索数据并显示数据的相关代码:

// The class used to create the SQLite table
public class PhotraxBaseData
{
    [SQLite.PrimaryKey]
    [SQLite.AutoIncrement]
    public int Id { get; set; }
    . . .
    public DateTime dateTimeTaken { get; set; }
    public String filePath { get; set; }
    public Byte[] thumbnail { get; set; }
}

private async void Gener8MapMarkers(List<PhotraxBaseData> _pbd)
{
    foreach (PhotraxBaseData pbd in _pbd)
    {
        String descAndFilename = Path.GetFileName(pbd.filePath);

        if (null != pbd.imgDescription)
        {
            descAndFilename = String.Format("{0} - {1}", pbd.imgDescription, descAndFilename);
        }
        BitmapImage bmi = await PhotraxUtils.ByteArrayToBitmapImage(pbd.thumbnail);
        if (PhotraxUtils.ValidLatAndLong(pbd.latitude, pbd.longitude))
        {
            Location loc = new Location(pbd.latitude, pbd.longitude);
            AddPushpin(loc, descAndFilename, pbd.dateTimeTaken, null, DataLayer);
        }
    }
}

public static async Task<BitmapImage> ByteArrayToBitmapImage(this byte[] byteArray)
{
    // from http://dotnetspeak.com/2013/04/convert-byte-array-to-an-image-in-winrt
    if (byteArray != null)
    {
        using (var stream = new InMemoryRandomAccessStream())
        {
            await stream.WriteAsync(byteArray.AsBuffer());
            var image = new BitmapImage();
            stream.Seek(0);
            image.SetSource(stream);
            return image;
        }
    }
    return null;
}

public class PushpinMetadata
{
    public string DescAndFileName { get; set; }
    public DateTime DateTimeTaken { get; set; }
    public BitmapImage Thumbnail { get; set; }
}

public void AddPushpin(Location latlong, string descAndFileName, DateTime dateTimeTaken, BitmapImage thumbnail, MapLayer layer)
{
    Pushpin p = new Pushpin()
    {
        Tag = new PushpinMetadata()
        {
            DescAndFileName = descAndFileName,
            DateTimeTaken = dateTimeTaken,
            Thumbnail = thumbnail
        }
    };

    MapLayer.SetPosition(p, latlong);
    p.Tapped += PinTapped;
    layer.Children.Add(p);
}

private void PinTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
    Pushpin p = sender as Pushpin;
    PushpinMetadata m = (PushpinMetadata)p.Tag;

    //Ensure there is content to be displayed
    if (!String.IsNullOrEmpty(m.DescAndFileName))
    {
        Infobox.DataContext = m;
        Infobox.Visibility = Visibility.Visible;
        MapLayer.SetPosition(Infobox, MapLayer.GetPosition(p));
    }
    else
    {
        Infobox.Visibility = Visibility.Collapsed;
    }
}

XAML:

<bm:Map.Children>
    <!-- Data Layer-->
    <bm:MapLayer Name="DataLayer"/>
    <!--Common Infobox-->
    <bm:MapLayer>
        <Grid x:Name="Infobox" Visibility="Collapsed" Margin="0,-115,-15,0">
            <Border Width="300" Height="110" Background="Black" Opacity="0.8" BorderBrush="White" BorderThickness="2" CornerRadius="5"/>
            <StackPanel Height="100" Margin="5">
                <Grid Height="40">
                    <TextBlock Text="{Binding DescAndFileName}" FontSize="20" Width="250" TextWrapping="Wrap" HorizontalAlignment="Left" />
                    <Button Content="X" Tapped="CloseInfobox_Tapped" HorizontalAlignment="Right" VerticalAlignment="Top"/>
                </Grid>
                <Grid Height="40">
                    <Viewbox Height="200" Stretch="Uniform" StretchDirection="Both">
                        <Image Source="{Binding Thumbnail}" Width="Auto" Height="Auto" Margin="2"/>
                    </Viewbox>
                </Grid>
                <Grid Height="40">
                    <TextBlock Text="{Binding DateTimeTaken}" FontSize="16" Width="290" TextWrapping="Wrap" Height="Auto"/>
                </Grid>
            </StackPanel>
        </Grid>
    </bm:MapLayer>
    <callisto:CustomDialog Content="CustomDialog" Height="100" Width="100"/>
</bm:Map.Children>

我的猜测是Image的Source属性不太知道如何处理它通过“Thumbnail”绑定的BitmapImage;但我不知道如何解决不匹配问题(假设存在问题所在。)

更新

要回答Faye的兄弟Chris,这里是“数据库”(SQLite类)声明:

public Byte[] thumbnail { get; set; }

这是我调用转换器方法的地方,传递类实例的相应成员,然后添加图钉:

BitmapImage bmi = await PhotraxUtils.ByteArrayToBitmapImage(pbd.thumbnail);
if (PhotraxUtils.ValidLatAndLong(pbd.latitude, pbd.longitude))
{
    Location loc = new Location(pbd.latitude, pbd.longitude);
    //AddPushpin(loc, descAndFilename, pbd.dateTimeTaken, null, DataLayer);
    AddPushpin(loc, descAndFilename, pbd.dateTimeTaken, bmi, DataLayer);
}

然而:我的坏人,人们!!!

我忽略了用“bmi”替换我的占位符“null”arg(你可以看到我上面的代码在上面注释掉了,我的新工作调用了它下面的AddPushpin())。图像的大小/比例都是错误的,但我认为这很容易修复。

我不知道是喜欢跳跃还是踢自己,所以我想我会同时做两件事。

感谢Dunaway先生让我更仔细地查看我的代码 - 在发布之前我显然应该做的事情。

0 个答案:

没有答案