2017-02-17 74 views
0

我已經成功在Windows Imaging組件中加載瞭如下的圖像但是我在最後一步被卡住了,如何保存現在可用於pWICBitmap變量的圖像。我試圖用Windows成像組件加載一個32位圖像,翻轉它,然後再次將它保存回到同一個文件中

另外我想知道是否,下面是翻轉位圖圖像的好方法嗎?

這裏是我的代碼:

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); 

回答

0

創建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

相關問題