Direktes Lesen und Schreiben in nicht verwalteten Bitmap-Speicher (Scan0)

Ist es in Ordnung, direkt aus einem nicht verwalteten Bitmap-Speicher zu schreiben und zu lesen?

Kann ich die BitmapData weiter verwenden, nachdem ich BitmapBits entsperrt habe? Ich habe eine Test-App erstellt, in der ich die Pixel der Bitmap einer PictureBox an der Mausposition lesen kann, während ein anderer Thread Pixel in dieselbe Bitmap schreibt.

EDIT 1: WieBoing haben in seiner Antwort darauf hingewiesen: "Scan0 zeigt nicht auf die tatsächlichen Pixeldaten des Bitmap-Objekts, sondern auf einen temporären Puffer, der einen Teil der Pixeldaten im Bitmap-Objekt darstellt." vonMSDN.

Aber sobald ich den Scan0 habe, kann ich die Bitmap lesen / schreiben, ohne Lockbits oder UnlockBits zu benötigen! Ich mache das oft in einem Thread. Entsprechend MSDN sollte es nicht passieren, da Scan0 auf ein COPY der Bitmap-Daten zeigt! Nun, in C # zeigt jeder Test, dass es sich nicht um eine Kopie handelt. In C ++ weiß ich nicht, ob es so funktioniert, wie es sollte.

EDIT 2: Wenn Sie die Methode "Drehen" verwenden, wird das Betriebssystem manchmal veranlasst, die Bitmap-Pixeldatenkopie freizugeben. Fazit,it is not safe to read/write an unlocked Bitmap Scan0. Danke Boing für deine Antwort und Kommentare!

Im Folgenden wird beschrieben, wie ich die BitmapData erhalte und den Pixelwert lese und schreibe.

    /// <summary>
    /// Locks and unlocks the Bitmap to get the BitmapData.
    /// </summary>
    /// <param name="bmp">Bitmap</param>
    /// <returns>BitmapData</returns>
    public static BitmapData GetBitmapData(Bitmap bmp)
    {
        BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
        bmp.UnlockBits(bmpData);
        return bmpData;
    }

    /// <summary>
    /// Get pixel directly from unamanged pixel data based on the Scan0 pointer.
    /// </summary>
    /// <param name="bmpData">BitmapData of the Bitmap to get the pixel</param>
    /// <param name="p">Pixel position</param>
    /// <param name="channel">Channel</param>
    /// <returns>Pixel value</returns>
    public static byte GetPixel(BitmapData bmpData, Point p, int channel)
    {
        if ((p.X > bmpData.Width - 1) || (p.Y > bmpData.Height - 1))
            throw new ArgumentException("GetPixel Point p is outside image bounds!");

        int bitsPerPixel = ((int)bmpData.PixelFormat >> 8) & 0xFF;
        int bpp = bitsPerPixel / 8;
        byte data;
        int id = p.Y * bmpData.Stride + p.X * bpp;
        unsafe
        {
            byte* pData = (byte*)bmpData.Scan0;
            data = pData[id + channel];
        }
        return data;
    }

    //Non UI Thread
    private void DrawtoBitmapLoop()
    {
        while (_drawBitmap)
        {
            _drawPoint = new Point(_drawPoint.X + 10, _drawPoint.Y + 10);
            if (_drawPoint.X > _backImageData.Width - 20)
                _drawPoint.X = 0;
            if (_drawPoint.Y > _backImageData.Height - 20)
                _drawPoint.Y = 0;

            DrawToScan0(_backImageData, _drawPoint, 1);

            Thread.Sleep(10);
        }
    }

    private static void DrawToScan0(BitmapData bmpData, Point start, int channel = 0)
    {
        int x = start.X;
        int y = start.Y;
        int bitsPerPixel = ((int)bmpData.PixelFormat >> 8) & 0xFF;
        int bpp = bitsPerPixel / 8;
        for (int i = 0; i < 10; i++)
        {
            unsafe
            {
                byte* p = (byte*)bmpData.Scan0;
                int id = bmpData.Stride * y + channel + (x + i) * bpp;
                p[id] = 255;
            }
        }
    }

Antworten auf die Frage(1)

Ihre Antwort auf die Frage