2016-07-11 36 views
1

WPFで作業し、BitmapSourceをゼロから作成しようとしています。次のコードでは、 "値が期待範囲内に収まらない"という引数例外があります。なぜ私は理解できません。私はまだWPFに新しく、おそらく私は正しくピクセル形式を使用していないのですか?BitmapSource.Createで値が範囲外です。

int width = 12; 
int height = 14; 
byte[] colorData = new byte[width * height * 4]; 
for(int i = 0; i < colorData.Length; i++) 
{ 
    colorData[i] = 155; //create all pixels same shade of gray 
} 
BitmapSource bitmap = BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgra32, null, colorData, width); 

答えて

2

BitmapSource.Create方法の最後の引数、すなわち「走査線」あたりのバイト数ストライド、です。

ピクセル当たり4バイトのピクセルフォーマットの場合には、4 * widthある:

var stride = 4 * width; 
var bitmap = BitmapSource.Create(
    width, height, 96, 96, PixelFormats.Bgra32, null, colorData, stride); 
関連する問題