代码改变世界

WP7备注(8)(WebClient读取图片)

2011-04-26 17:03  血糯米Otomii  阅读(404)  评论(0编辑  收藏  举报

 

远程图片数据申请:

XNA:

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += OnWebClientOpenReadCompleted;
webClient.OpenReadAsync(new
Uri("http://www.charlespetzold.com/Media/HelloWP7.jpg"));
}
void OnWebClientOpenReadCompleted(object sender, OpenReadCompletedEventArgs args)
{
if (!args.Cancelled && args.Error == null)
{
helloTexture = Texture2D.FromStream(this.GraphicsDevice, args.Result);
}
}

Silverlight:

protected override void OnManipulationStarted(ManipulationStartedEventArgs args)
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += OnWebClientOpenReadCompleted;
webClient.OpenReadAsync(new
Uri("http://www.charlespetzold.com/Media/HelloWP7.jpg"));
args.Complete();
args.Handled = true;
base.OnManipulationStarted(args);
}
void OnWebClientOpenReadCompleted(object sender, OpenReadCompletedEventArgs args)
{
if (!args.Cancelled && args.Error == null)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(args.Result);
img.Source = bmp;
}
}

图片编译进入DLL字符串格式:

"/SilverlightDemo;component/Images/HelloWorld.png"