Silverlight通过uri创建BitmapImage总是出现异常

var source = new BitmapImage(new Uri("/HeatMap;component/Palette/1.png", UriKind.Relative));

如上创建BitmapImage,发现source的PixelHeight和 PixelHeight均为0,这该如何是好呢?直接创建不行,间接呢?蓦然发现BitmapImage有个SetSource()方法,他只需要流参数,那就简单了,根据先通过uri创建个流

System.Windows.Resources.StreamResourceInfo resourceInfo = Application.GetResourceStream(new System.Uri("/HeatMap;component/Palette/1.png", UriKind.Relative));
var image= new BitmapImage();
image.SetSource(resourceInfo.Stream);

监视image变量发现PixelHeight和PixelHeight不再是0,接下来我们就可以用这个imageSource 做我们想做的事了,只是为啥会这样呢,暂时没想通。

原文地址:https://www.cnblogs.com/alirong/p/3158712.html