WriteableBitmap 在 Silverlight 4 中有新功能吗?

发布于 2024-09-13 02:46:15 字数 321 浏览 1 评论 0 原文

如果有人能给我一个关于如何使用它的例子,通过流? (不确定它是如何工作的)我知道如何从 URI 创建 BitmapImage 现在我需要将此图像转换为 WriteableBitmap,但是我收到一个空异常错误,如下所示:

BitmapImage image = new BitmapImage(new Uri("http://www.example.com/example.png"));
WriteableBitmap newImage = new WriteableBitmap(image);

and if someone could give me an example on how to use it, with a stream? (not sure how that works) I know how to create a BitmapImage from an URI now I need to convert this image to a WriteableBitmap, but I get a null exception error with something like this:

BitmapImage image = new BitmapImage(new Uri("http://www.example.com/example.png"));
WriteableBitmap newImage = new WriteableBitmap(image);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2024-09-20 02:46:15

简而言之:不,Silverlight 4 中没有新功能。 WriteableBitmapEx 尝试弥补缺失的功能。

关于您的实际问题:
您应该将处理程序添加到 BitmapImage.ImageFailed 事件以查看下载图像时是否出现错误。您应该在 ImageOpened 事件处理程序。

var image = new BitmapImage(new Uri("http://www.example.com/example.png"));
WriteableBitmap newImage = null;
image.ImageOpened += (s, e) => newImage = new WriteableBitmap(image);

另请注意,允许跨域引用。有关详细信息,请参阅 MSDN 页面您应该将图像放入 Web 项目的 ClientBin 文件夹中并使用相对路径。

作为替代方案,您还可以将图像作为资源编译到程序集中并从那里加载。 WriteableBitmapEx 有一个扩展方法可以使此任务变得更容易一些。但请记住,这会增大程序集大小,并且初始 XAP 加载时间也会增加。

// Load an image from the calling Assembly's resources only by passing the relative path
var writeableBmp = new WriteableBitmap(0, 0).FromResource("example.png");

In short: Nope, there are no new features in Silverlight 4. The WriteableBitmapEx tries to compensate the missing functionality.

Regarding your real problem:
You should add a handler to the BitmapImage.ImageFailed event to see if there's an error when the image should be downloaded. And you you should create the WriteableBitmap in the ImageOpened event handler.

var image = new BitmapImage(new Uri("http://www.example.com/example.png"));
WriteableBitmap newImage = null;
image.ImageOpened += (s, e) => newImage = new WriteableBitmap(image);

Please also note that cross-domain references are permitted. See the MSDN page for details. You should put the image into the Web Project's ClientBin folder and use a relative path instead.

As an alternative you can also compile the image into the assembly as resource and load it from there. The WriteableBitmapEx has an extension method to make this task a bit easier. But keep in mind that this blows the assembly size up and the initial XAP loading time will increase.

// Load an image from the calling Assembly's resources only by passing the relative path
var writeableBmp = new WriteableBitmap(0, 0).FromResource("example.png");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文