最近、低レベルのグラフィックス プログラミングの仕事 (ラスター グラフィックス アルゴリズムの実装) が必要になりました。unsafe
そのため、低レベルの抽象化 (コードを使用しても) でプリミティブ (線、四角形、楕円など) を描画するのに役立つツール (クラス) を探し始めました。
アルゴリズムをWPF、WindowsForms、WinAPIまたはその他の環境で実装する必要があるかどうかを検討します。とりあえずWPFを試してみます。ビットマップにピクセルを描画する方法の例をいくつか見つけましたが、残念ながらこのコードを理解するのに問題があります( MSDNのソースコード):
// The DrawPixel method updates the WriteableBitmap by using
// unsafe code to write a pixel into the back buffer.
static void DrawPixel(MouseEventArgs e)
{
int column = (int)e.GetPosition(i).X;
int row = (int)e.GetPosition(i).Y;
// Reserve the back buffer for updates.
writeableBitmap.Lock();
unsafe
{
// Get a pointer to the back buffer.
int pBackBuffer = (int)writeableBitmap.BackBuffer;
// Find the address of the pixel to draw.
pBackBuffer += row * writeableBitmap.BackBufferStride;
pBackBuffer += column * 4;//??
// Compute the pixel's color.
int color_data = 255 << 16; // R
color_data |= 128 << 8; // G
color_data |= 255 << 0; // B
// Assign the color data to the pixel.
*((int*)pBackBuffer) = color_data;//??
}
// Specify the area of the bitmap that changed.
writeableBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));
// Release the back buffer and make it available for display.
writeableBitmap.Unlock();
}
- この
pBackBuffer += column * 4;
行は何のためですか?なぜ4
ですか? - それ
*((int*)pBackBuffer) = color_data;
はどういう意味ですか?私はC/C++からのポインターを知っていますが、C#には がIntPtr
あります。int*
この行は、int pBackBuffer = (int)writeableBitmap.BackBuffer;
同等に扱うことができることを示唆してint
おり、IntPtr
これも明確ではありません。 - どのプログラミング環境を使用すればよいですか?
WinAPI
、WPF
または他の?
誰かがこの安全でないコードを説明してくれたら、とてもありがたいです。