Lire en anglais

Partager via


Bitmap.UnlockBits(BitmapData) Méthode

Définition

Déverrouille ce Bitmap dans la mémoire système.

C#
public void UnlockBits (System.Drawing.Imaging.BitmapData bitmapdata);

Paramètres

bitmapdata
BitmapData

BitmapData qui spécifie des informations sur l'opération de verrouillage.

Exceptions

L'opération a échoué.

Exemples

L’exemple de code suivant montre comment utiliser les PixelFormatpropriétés , Height, Widthet Scan0 ; les LockBits méthodes et UnlockBits ; et l’énumération ImageLockMode . Cet exemple n’est pas conçu pour fonctionner correctement avec tous les formats de pixels, mais pour fournir un exemple d’utilisation de la LockBits méthode. Cet exemple est conçu pour être utilisé avec Windows Forms. Pour exécuter cet exemple, collez-le dans un formulaire et gérez l’événement du Paint formulaire en appelant la LockUnlockBitsExample méthode, en passant e comme PaintEventArgs.

C#
private void LockUnlockBitsExample(PaintEventArgs e)
    {

        // Create a new bitmap.
        Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");

        // Lock the bitmap's bits.  
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData =
            bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            bmp.PixelFormat);

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap.
        int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
        byte[] rgbValues = new byte[bytes];

        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

        // Set every third value to 255. A 24bpp bitmap will look red.  
        for (int counter = 2; counter < rgbValues.Length; counter += 3)
            rgbValues[counter] = 255;

        // Copy the RGB values back to the bitmap
        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

        // Unlock the bits.
        bmp.UnlockBits(bmpData);

        // Draw the modified image.
        e.Graphics.DrawImage(bmp, 0, 150);
    }

Remarques

spécifie BitmapData les attributs du , tels que la taille, le Bitmapformat des pixels, l’adresse de départ des données de pixel en mémoire et la longueur de chaque ligne d’analyse (foulée).

S’applique à

Produit Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9