5

生のバイトからビットマップ オブジェクトを作成しようとしていますPixelFormatRGBサンプルあたり 8 ビットで、ピクセルあたり 3 バイトです。これで、私の歩幅は幅の 3 倍になります。

しかし、Bitmapクラスは常にストライド値の乗数 4 を探しています。この問題を解決する方法を教えてください。4 の乗数を指定すると、画像が正しく表示されません。

Bitmap im = new Bitmap(MyOBJ.PixelData.Columns, MyOBJ.PixelData.Rows, (MyOBJ.PixelData.Columns*3),
System.Drawing.Imaging.PixelFormat.Format24bppRgb, Marshal.UnsafeAddrOfPinnedArrayElement(images[imageIndex], 0));
4

1 に答える 1

7

配列のすべての行をパディングして、必要な形式に適合させる短いサンプルを作成しました。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;
}
于 2012-12-30T17:31:03.260 に答える