我尝试使用Windows Imaging Component加载32位图像,将其翻转然后再将其保存在同一个文件中

时间:2017-02-17 14:37:14

标签: c++ bitmap wic

我已成功将图像加载到Windows Imaging组件中,如下所示但是我在最后一步陷入困境,如何保存pWICBitmap变量中现在可用的图像。

另外我想知道是否,是否有一个翻转Bitmap图像的好方法?

这是我的代码:

IWICImagingFactory *pIWICFactory = NULL;
IWICFormatConverter* pWICConv = NULL;           //WIC converter
IWICBitmapDecoder *pIDecoder = NULL;
IWICBitmapFrameDecode *pIDecoderFrame  = NULL;
IWICBitmapFlipRotator *pIFlipRotator = NULL;
IWICBitmap *pWICBitmap = NULL;

CoInitializeEx(0, COINIT_MULTITHREADED);
HRESULT hr = CoCreateInstance(
            CLSID_WICImagingFactory,
            NULL,
            CLSCTX_INPROC_SERVER,
            IID_PPV_ARGS(&pIWICFactory)
            );


hr = pIWICFactory->CreateDecoderFromFilename(
   //(LPCWSTR)pszBMPFilePath,                  // Image to be decoded
   L"Bitmap.bmp",
   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
   );

if (SUCCEEDED(hr))
{
   hr = pIDecoder->GetFrame(0, &pIDecoderFrame);
}

//Create a format converter using the IWICImagingFactory to convert the 
//image data from one pixel format to another, handling dithering and 
//halftoning to indexed formats, palette translation and alpha thresholding.
if (SUCCEEDED(hr)) {
    hr = pIWICFactory->CreateFormatConverter(&pWICConv);
}

//Initialize the format converter with all sorts of information, including the frame that was
//decoded above
if (SUCCEEDED(hr))
    hr = pWICConv->Initialize(pIDecoderFrame,
        GUID_WICPixelFormat32bppPBGRA,   // Destination pixel format
        WICBitmapDitherTypeNone,
        NULL,
        0.f,
        WICBitmapPaletteTypeMedianCut);


if (SUCCEEDED(hr))
{
   hr = pIWICFactory->CreateBitmapFlipRotator(&pIFlipRotator);
}

    // 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.
    hr = pIFlipRotator->Initialize(
        pWICConv,                     // Bitmap source to flip.
        WICBitmapTransformFlipHorizontal);  // Flip the pixels along the vertical y-axis.

    /*hr = pIFlipRotator->Initialize(
        pIDecoderFrame,
        WICBitmapTransformFlipVertical);*/
    /*hr = pIFlipRotator->Initialize(
        pIDecoderFrame,
        WICBitmapTransformRotate180);*/
}

//Create the WICBitmap (mpImgWIC) from the Bitmap source (WIC Flip rotator)
hr = pIWICFactory->CreateBitmapFromSource(pIFlipRotator, WICBitmapCacheOnLoad, &pWICBitmap);

1 个答案:

答案 0 :(得分:1)

不需要创建WICBitmap。 IWICBitmapFlipRotator是一个IWICBitmapSource,可用于创建图像。

您需要创建IWICBitmapEncoder和IWICBitmapFrameEncode对象。

MSDN在此处有一个示例:https://msdn.microsoft.com/en-us/library/windows/desktop/gg430021(v=vs.85).aspx

MSDN的示例预计您将手动提供图像数据,但您可以使用IWICBitmapFrameEncode :: WriteSource方法直接从IWICBitmapFlipRotator写入:https://msdn.microsoft.com/en-us/library/windows/desktop/ee690159(v=vs.85).aspx