Condividi tramite


Come ritagliare una sorgente bitmap

Questo argomento illustra come ottenere una parte rettangolare di un IWICBitmapSource usando un componente IWICBitmapClipper.

Per ritagliare un'origine bitmap

  1. Creare un oggetto IWICImagingFactory per creare oggetti Windows Imaging Component (WIC).

    // Create WIC factory
    hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&m_pIWICFactory)
        );
    
  2. Utilizzare il metodoCreateDecoderFromFilenameper creare unIWICBitmapDecoder da un file di immagine.

    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. Ottenere il primo IWICBitmapFrameDecode dell'immagine.

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

    Il formato di file JPEG supporta solo un singolo fotogramma. Poiché il file in questo esempio è un file JPEG, viene usato il primo frame (0). Per i formati di immagine con più fotogrammi, vedere Come recuperare i fotogrammi di un'immagine per accedere a ogni fotogramma dell'immagine.

  4. Creare il IWICBitmapClipper da usare per il ritaglio dell'immagine.

    IWICBitmapClipper *pIClipper = NULL;
    
    if (SUCCEEDED(hr))
    {
       hr = m_pIWICFactory->CreateBitmapClipper(&pIClipper);
    }
    
  5. Inizializzare l'oggetto clipper con i dati dell'immagine all'interno del rettangolo specificato del frame bitmap.

    // Create the clipping rectangle.
    WICRect rcClip = { 0, 0, uiWidth/2, uiHeight/2 };
    
    // Initialize the clipper with the given rectangle of the frame's image data.
    if (SUCCEEDED(hr))
    {
       hr = pIClipper->Initialize(pIDecoderFrame, &rcClip);
    }
    
  6. Disegnare o elaborare l'immagine ritagliata.

    La figura seguente illustra il ritaglio dell'immagine. L'immagine originale a sinistra è 200 x 130 pixel. L'immagine a destra è l'immagine originale ritagliata in un rettangolo definito come {20,20,100,100}.

    illustrazione del ritaglio dell'immagine

Vedere anche

Guida alla programmazione

Riferimento

esempi di