2016-11-16 11 views
0

私はビットマップに慣れていないので、ビットマップとしてFrameworkElement(specificaly Grid)を保存してバッファにコピーする必要があります。問題は、RenderTargetBitmapでサポートされていないPgrbaではなく、Rgba形式で保存する必要があることです。関連するコードは次のとおりです。RenderTargetBitmap with format rgba32

_targetBitmap = new RenderTargetBitmap(xres, yres, 96, 96, PixelFormats.Pbgra32); 
_targetBitmap.Clear(); 
// Child is grid 
_targetBitmap.Render(Child); 
// copy the pixels into the buffer 
_targetBitmap.CopyPixels(new Int32Rect(0, 0, xres, yres), bufferPtr, _bufferSize, _stride); 

私はWriteableBitmapを使用しようとしましたが、子をレンダリングする方法はありませんでした。助言がありますか?

+0

明らかに、WPFはRgba32をまったくサポートしていません。では、WriteableBitmapはどのように役立つはずですか? – Clemens

+0

ああ、私はそう思った。私の悪い。 – Korhak

答えて

1

CopyPixels関数は、既にピクセルデータに直接アクセスできるようにしているので、フォーマット間の変換が必要です。この場合、チャネルオーダーを交換して、アルファ値の事前増倍を元に戻す必要があります。

注:このコードでは、bufferPtrがバイト配列またはバイトポインタであることを前提としています。

for (int y = 0; y < yres; y++) 
{ 
    for (int x = 0; x < xres; x++) 
    { 
     // Calculate array offset for this pixel 
     int offset = y * _stride + x * 4; 

     // Extract individual color channels from pixel value 
     int pb = bufferPtr[offset]; 
     int pg = bufferPtr[offset + 1]; 
     int pr = bufferPtr[offset + 2]; 
     int alpha = bufferPtr[offset + 3]; 

     // Remove premultiplication 
     int r = 0, g = 0, b = 0; 
     if (alpha > 0) 
     { 
      r = pr * 255/alpha; 
      g = pg * 255/alpha; 
      b = pb * 255/alpha; 
     } 

     // Write color channels in desired order 
     bufferPtr[offset] = (byte)r; 
     bufferPtr[offset + 1] = (byte)g; 
     bufferPtr[offset + 2] = (byte)b; 
     bufferPtr[offset + 3] = (byte)alpha; 
    } 
}