- Achtung: Das Lock/Unlock des Bitmaps sollte normalerweise nur einmal vor/nach Operationen am Bitmap erfolgen und nicht jedesmal direkt in der Funktion selber, da dies zusätzliche Zeit benötigt.
- eine sehr interessante Klasse für den Zugriff auf Bitmaps einschließlich diverser Funktionen gibt es hier: Link
private struct SPixelData
{
public byte R;
public byte G;
public byte B;
}
// Funktion ist unsafe, da Pointer verwendet werden
public unsafe int GetGreyValue(Bitmap b, int x, int y)
{
int iGrey = 0;
// Bildgröße herausfinden
Rectangle r = new Rectangle(Point.Empty, b.Size);
int iWidth = (int)(r.Width * sizeof(SPixelData));
// Bitmap locken
BitmapData bdData = b.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
// Zeiger auf das Bild holen
byte* pBase = (byte*)bdData.Scan0.ToPointer();
// Zeiger auf das Pixel n der Stelle x,y holen
SPixelData* pPixel = (SPixelData*)(pBase + y * iWidth + x * sizeof(SPixelData));
// Grauwert ausrechnen
iGrey = (int)((pPixel->R * 0.3) + (pPixel->G * 0.59) + (pPixel->B * 0.11));
// Bitmap wieder freigeben
b.UnlockBits(bdData);
return iGrey;
}