Partager via


Comment modifier les pixels d’une source bitmap

Cette rubrique montre comment modifier les pixels d’une source bitmap à l’aide des composants IWICBitmap et IWICBitmapLock .

Pour modifier les pixels d’une source bitmap

  1. Créez un objet IWICImagingFactory pour créer des objets WIC (Windows Imaging Component).

    // Create WIC factory
    hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&m_pIWICFactory)
        );
    
  2. Utilisez la méthode CreateDecoderFromFilename pour créer un IWICBitmapDecoder à partir d’un fichier image.

    HRESULT hr = S_OK;
    
    IWICBitmapDecoder *pIDecoder = NULL;
    IWICBitmapFrameDecode *pIDecoderFrame  = NULL;
    
    hr = m_pIWICFactory->CreateDecoderFromFilename(
       L"turtle.jpg",                  // Image to be decoded
       NULL,                           // Do not prefer a particular vendor
       GENERIC_READ,                   // Desired read access to the file
       WICDecodeMetadataCacheOnDemand, // Cache metadata when needed
       &pIDecoder                      // Pointer to the decoder
       );
    
  3. Obtenez le premier IWICBitmapFrameDecode de l’image.

    // Retrieve the first bitmap frame.
    if (SUCCEEDED(hr))
    {
       hr = pIDecoder->GetFrame(0, &pIDecoderFrame);
    }
    

    Le format de fichier JPEG ne prend en charge qu’une seule image. Étant donné que le fichier dans cet exemple est un fichier JPEG, le premier frame (0) est utilisé. Pour les formats d’image qui ont plusieurs images, consultez Comment récupérer les images d’une image pour accéder à chaque image de l’image.

  4. Créez un IWICBitmap à partir du frame d’image obtenu précédemment.

    IWICBitmap *pIBitmap = NULL;
    IWICBitmapLock *pILock = NULL;
    
    UINT uiWidth = 10;
    UINT uiHeight = 10;
    
    WICRect rcLock = { 0, 0, uiWidth, uiHeight };
    
    // Create the bitmap from the image frame.
    if (SUCCEEDED(hr))
    {
       hr = m_pIWICFactory->CreateBitmapFromSource(
          pIDecoderFrame,          // Create a bitmap from the image frame
          WICBitmapCacheOnDemand,  // Cache bitmap pixels on first access
          &pIBitmap);              // Pointer to the bitmap
    }
    
  5. Obtenez un IWICBitmapLock pour un rectangle spécifié du IWICBitmap.

    if (SUCCEEDED(hr))
    {
       // Obtain a bitmap lock for exclusive write.
       // The lock is for a 10x10 rectangle starting at the top left of the
       // bitmap.
       hr = pIBitmap->Lock(&rcLock, WICBitmapLockWrite, &pILock);
    
  6. Traitez les données de pixel qui sont maintenant verrouillées par l’objet IWICBitmapLock .

       if (SUCCEEDED(hr))
       {
          UINT cbBufferSize = 0;
          BYTE *pv = NULL;
    
          // Retrieve a pointer to the pixel data.
          if (SUCCEEDED(hr))
          {
             hr = pILock->GetDataPointer(&cbBufferSize, &pv);
          }
    
          // Pixel manipulation using the image data pointer pv.
          // ...
    
          // Release the bitmap lock.
          SafeRelease(&pILock);
       }
    }
    

    Pour déverrouiller le IWICBitmap, appelez IUnknown::Release sur tous les objets IWICBitmapLock associés à IWICBitmap.

  7. Nettoyer les objets créés.

    SafeRelease(&pIBitmap);
    SafeRelease(&pIDecoder);
    SafeRelease(&pIDecoderFrame);
    

Voir aussi

Guide de programmation

Référence

Exemples