Direct2D Intro - Flip and Rotate a Bitmap Source

本文演示了如何使用WIC库通过IWICBitmapFlipRotator组件旋转和翻转IWICBitmapSource对象。包括创建WIC工厂、加载图像文件、获取图像帧、初始化翻转/旋转器并应用翻转操作。


https://msdn.microsoft.com/en-us/library/windows/desktop/ee719659(v=vs.85).aspx


How to Flip and Rotate a Bitmap Source

This topic demonstrates how to rotate an IWICBitmapSource by using an IWICBitmapFlipRotator component.

To flip and rotate a bitmap source

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

    C++
    // Create WIC factory
    hr = CoCreateInstance(
        CLSID_WICImagingFactory,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&m_pIWICFactory)
        );
    
    
    
  2. Use the CreateDecoderFromFilename method to create an IWICBitmapDecoder from an image file.

    C++
    HRESULT hr = S_OK;
    
    IWICBitmapDecoder *pIDecoder = NULL;
    IWICBitmapFrameDecode *pIDecoderFrame  = NULL;
    IWICBitmapFlipRotator *pIFlipRotator = 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.

    C++
    // 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 IWICBitmapFlipRotator to use for flipping the image.

    C++
    // Create the flip/rotator.
    if (SUCCEEDED(hr))
    {
       hr = m_pIWICFactory->CreateBitmapFlipRotator(&pIFlipRotator);
    }
    
    
    
  5. Initialize the flip/rotator object with the image data of the bitmap frame flipped horizontally (along the vertical y-axis).

    C++
    // Initialize the flip/rotator to flip the original source horizontally.
    if (SUCCEEDED(hr))
    {
       hr = pIFlipRotator->Initialize(
          pIDecoderFrame,                     // Bitmap source to flip.
          WICBitmapTransformFlipHorizontal);  // Flip the pixels along the 
                                              //  vertical y-axis.
    }
    
    
    

    See WICBitmapTransformOptions for additional rotations and flipping options.

  6. Draw or process the flipped bitmap source.

    Note  The IWICBitmapFlipRotator interface inherits from the IWICBitmapSource interface, so you can use the initialized flip/rotator object anywhere that accepts a IWICBitmapSource.
              

The following illustration demonstrates flipping an imaging horizontally (along the vertical x-axis).

Illustration showing a horizontal flip (along the verital x-axis) of an image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值