【问题标题】:Does WriteableBitmap has new features in Silverlight 4?WriteableBitmap 在 Silverlight 4 中是否有新功能?
【发布时间】:2010-08-03 22:03:52
【问题描述】:

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

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

【问题讨论】:

  • 你想用来自网络文件或资源流的PNG图像填充WriteableBitmap吗?
  • 我在网页上有一个 PNG,我想用它来填充 WriteableBitmap,我不在乎如何。但是我需要修改图像(在像素级别,PNG 的 8 位会更容易)使用资源流更好吗?

标签: c# silverlight writeablebitmap


【解决方案1】:

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

关于您的真正问题: 您应该向BitmapImage.ImageFailed 事件添加一个处理程序,以查看在下载图像时是否有错误。您应该在 ImageOpened 事件处理程序中创建 WriteableBitmap。

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

另请注意,允许跨域引用。有关详细信息,请参阅MSDN page您应该将图像放入 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");

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多