配列のすべての行をパディングして、必要な形式に適合させる短いサンプルを作成しました。2x2 チェック ボード ビットマップを作成します。
byte[] bytes =
{
255, 255, 255,
0, 0, 0,
0, 0, 0,
255, 255, 255,
};
var columns = 2;
var rows = 2;
var stride = columns*4;
var newbytes = PadLines(bytes, rows, columns);
var im = new Bitmap(columns, rows, stride,
PixelFormat.Format24bppRgb,
Marshal.UnsafeAddrOfPinnedArrayElement(newbytes, 0));
以下にそのPadLines
方法を書きます。Buffer.BlockCopy
ビットマップが大きい場合に備えて、最適化を試みました。
static byte[] PadLines(byte[] bytes, int rows, int columns)
{
//The old and new offsets could be passed through parameters,
//but I hardcoded them here as a sample.
var currentStride = columns*3;
var newStride = columns*4;
var newBytes = new byte[newStride*rows];
for (var i = 0; i < rows; i++)
Buffer.BlockCopy(bytes, currentStride*i, newBytes, newStride * i, currentStride);
return newBytes;
}