共用方式為


如何調整位圖來源

本主題示範如何使用 IWICBitmapScaler 元件來調整 IWICBitmapSource

縮放位圖來源

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

    // 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;
    IWICBitmapScaler *pIScaler = 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. 建立 IWICBitmapScaler,以用於影像縮放。

    // Create the scaler.
    if (SUCCEEDED(hr))
    {
       hr = m_pIWICFactory->CreateBitmapScaler(&pIScaler);
    }
    
  5. 將點陣圖框架的影像數據縮小為一半大小,以初始化縮放器物件。

    // Initialize the scaler to half the size of the original source.
    if (SUCCEEDED(hr))
    {
       hr = pIScaler->Initialize(
          pIDecoderFrame,                    // Bitmap source to scale.
          uiWidth/2,                         // Scale width to half of original.
          uiHeight/2,                        // Scale height to half of original.
          WICBitmapInterpolationModeFant);   // Use Fant mode interpolation.
    }
    
  6. 繪製或處理縮放位圖來源。

    下圖示範影像縮放比例。 左側的原始影像為 200 x 130 像素。 右邊的影像是縮放為大小一半的原始影像。

    圖例,顯示將影像調整為較小的大小

另請參閱

程式設計手冊

參考

範例