开发者

How do I load an image from a URL in Silverlight without having the image control be in the visual tree

To load an image from a URL in Silverlight and access its pixel information, you simply create an Image and set 开发者_如何学Gothe URL as its source and use that image to construct a WriteableBitmap. However, it seems that the Image control must be in the visual tree in order for the image to load. My dilemma is that I am creating an image processing library for Silverlight and I will not have access to the visual tree. Is there a way to get around this restriction?


I figured it out, this seems to work:

public void Fetch(Uri uri)
{
    WebClient webClient = new WebClient();
    webClient.OpenReadCompleted += this.ReadCompleted;
    webClient.OpenReadAsync(uri);
}

private void ReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    WebClient webClient = (WebClient)sender;
    webClient.OpenReadCompleted -= this.ReadCompleted;
    Stream stream = e.Result;
    BitmapImage bmp = new BitmapImage();
    bmp.SetSource(stream);
    WriteableBitmap wbmp = new WriteableBitmap(bmp);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜