How to Scale a Bitmap Source

How to Scale a Bitmap Source

This topic demonstrates how to scale an IWICBitmapSource using the IWICBitmapScaler component.

To scale a bitmap source

  1. Create an IWICImagingFactory object to create Windows Imaging Component (WIC) objects.

    // Create WIC factory
    hr = CoCreateInstance(
  2. Use the CreateDecoderFromFilename method to create an IWICBitmapDecoder from an image file.

    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. Get the first IWICBitmapFrameDecode of the image.

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

    The JPEG file format only supports a single frame. Because the file in this example is a JPEG file, the first frame (0) is used. For image formats that have multiple frames, see How to Retrieve the Frames of an Image for accessing each frame of the image.

  4. Create the IWICBitmapScaler to use for the image scaling.

    // Create the scaler.
    if (SUCCEEDED(hr))
       hr = m_pIWICFactory->CreateBitmapScaler(&pIScaler);
  5. Initialize the scaler object with the image data of the bitmap frame at half the size.

    // 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. Draw or process the scaled bitmap source.

    The following illustration demonstrates imaging scaling. The original image on the left is 200 x 130 pixels. The image on the right is the original image scaled to half the size.

    Illustration showing scaling an image to a smaller size

See Also

Programming Guide





© 2016 Microsoft