共用方式為


如何裁剪點陣圖來源

本主題示範如何使用IWICBitmapClipper元件取得IWICBitmapSource的矩形部分。

裁剪點陣圖來源

  1. 建立 IWICImagingFactory 物件,以在 WIC) 物件 (建立 Windows 映像元件。

    // Create WIC factory
    hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&m_pIWICFactory)
        );
    
  2. 使用 CreateDecoderFromFilename 方法,從影像檔建立 IWICBitmapDecoder

    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. 取得影像的第一個 IWICBitmapFrameDecode

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

    JPEG 檔案格式僅支援單一框架。 因為此範例中的檔案是 JPEG 檔案,所以會使用第一個框架 (0) 。 如需具有多個畫面的影像格式,請參閱如何擷取 影像的畫面格 ,以存取影像的每個畫面格。

  4. 建立要用於影像裁剪的 IWICBitmapClipper

    IWICBitmapClipper *pIClipper = NULL;
    
    if (SUCCEEDED(hr))
    {
       hr = m_pIWICFactory->CreateBitmapClipper(&pIClipper);
    }
    
  5. 使用點陣圖框架指定矩形內的影像資料,初始化 clipper 物件。

    // 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. 繪製或處理裁剪的影像。

    下圖示范影像裁剪。 左側的原始影像為 200 x 130 圖元。 右邊的影像是裁剪成定義為 {20,20,100,100} 的矩形的原始影像。

    影像裁剪圖例

另請參閱

程式設計指南

參考

範例