初始化WriteableBitmap时出现ArgumentException

时间:2015-07-25 14:58:06

标签: c# wpf

我试图在WPF中编写一个简单的程序,其中整个窗口由一个图像占用,该图像的源是我绘制的WriteableBitmap。以下是相关代码:

var image = m_Window.CustomImage;
image.Source = new WriteableBitmap(
    (int) image.ActualWidth,
    (int) image.ActualHeight,
    300,
    300,
    PixelFormats.Bgra32,
    null
);

但是当我运行它时,我在构造函数上得到System.ArgumentException: Value does not fall within the expected range。令人沮丧的是,它没有说哪个值不正确。我已经尝试了谷歌搜索,但是几乎没有一些WriteableBitmap构造函数的例子,那些确实存在的是首先生成上述代码的东西。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

问题是ActualWidth的{​​{1}}和ActualHeight属性为0,这将导致image

尝试将pixelWidth和pixelHeight属性的值设置为正整数,并查看异常是否消失。

ArgumentException