3

我正在尝试在运行时从 URI 加载 BitmapImage。我在我想通过数据绑定替换的 XAML 用户控件中使用默认图像。这行得通。

我遇到的问题是在替换图像使用无效文件的情况下(可能是错误的 URI,或者 URI 指定了非图像文件)。发生这种情况时,我希望能够检查 BitmapImage 对象以查看它是否已正确加载。如果没有,我想坚持使用的默认图像。

这是 XAML:

<UserControl x:Class="MyUserControl">
    <Grid>
        <Image
            x:Name="myIcon"
            Source="Images/default.png" />
    </Grid>
</UserControl>

以及相关的代码隐藏:

public static readonly DependencyProperty IconPathProperty =
    DependencyProperty.Register(
        "IconPath",
        typeof(string),
        typeof(MyUserControl),
        new PropertyMetadata(null, new PropertyChangedCallback(OnIconPathChanged)));

public string IconPath
{
    get { return (string)GetValue(IconPathProperty); }
    set { SetValue(IconPathProperty, value); }
}

private static void OnIconPathChanged(
    object sender,
    DependencyPropertyChangedEventArgs e)
{
    if (sender != null)
    {
        // Pass call through to the user control.
        MyUserControl control = sender as MyUserControl;
        if (control != null)
        {
            control.UpdateIcon();
        }
    }
}

public void UpdateIcon()
{
    BitmapImage replacementImage = new BitmapImage();

    replacementImage.BeginInit();
    replacementImage.CacheOption = BitmapCacheOption.OnLoad;

    // Setting the URI does not throw an exception if the URI is
    // invalid or if the file at the target URI is not an image.
    // The BitmapImage class does not seem to provide a mechanism
    // for determining if it contains valid data.
    replacementImage.UriSource = new Uri(IconPath, UriKind.RelativeOrAbsolute);

    replacementImage.EndInit();

    // I tried this null check, but it doesn't really work.  The replacementImage
    // object can have a non-null UriSource and still contain no actual image.
    if (replacementImage.UriSource != null)
    {
        myIcon.Source = replacementImage;
    }
}

下面是我如何在另一个 XAML 文件中创建此用户控件的实例:

<!--
  My problem:  What if example.png exists but is not a valid image file (or fails to load)?
-->
<MyUserControl IconPath="C:\\example.png" />

或者,也许有人可以建议一种不同/更好的方法来在运行时加载图像。谢谢。

4

4 回答 4

1

Well, BitmapImage class has two events, which will be raised when either download or decoding has failed.

yourBitmapImage.DownloadFailed += delegate { /* fall to your def image */ }
yourBitmapImage.DecodeFailed += delegate { /* fall to your def img */ }

On a side note, if you're trying to implement fallback placeholder: http://www.markermetro.com/2011/06/technical/mvvm-placeholder-images-for-failed-image-load-on-windows-phone-7-with-caliburn-micro/ seems nice.

于 2015-03-10T08:24:43.647 回答
1

这很粗糙,但我发现无效的 BitmapImage 的宽度和高度将为 0。

    BitmapImage image;
    if(image.Width > 0 && image.Height > 0) 
    {  
        //valid image 
    }     
于 2016-03-10T23:43:17.487 回答
0

你可以试试这个检查

        if (bitmapImage.UriSource==null || bitmapImage.UriSource.ToString()).Equals(""))
        {
            Console.WriteLine("path null");
        }
        else
        {
            bitmapImage.EndInit();
        }
于 2015-03-10T05:21:30.473 回答
-2

这已经得到了回答。请看一下这个这个。我认为他们俩都在某种程度上回答了您的问题,但我更喜欢前一种方法,因为它甚至会检查远程资源的 contentType。

你也可以看看这篇文章

更新:

如果是本地文件,可以通过简单地使用 Uri 或路径创建一个 Image 对象来检查这一点。如果成功,则表示该图像是有效图像:

try
{
    Image image = Image.FromFile(uri);
    image.Dispose();
}
catch (Exception ex)
{
    //Incorrect uri or filetype.
}
于 2012-04-20T07:48:51.783 回答