[英]I'm trying to load a 32 bit image with the Windows Imaging Component, flip it and then save it back again in the same file
我已如下所示將圖像成功加載到Windows Imaging組件中,但是我被困在最后一步,如何保存pWICBitmap變量中現在可用的圖像。
我也想知道是否下面是一種翻轉位圖圖像的好方法?
這是我的代碼:
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);
無需創建WICBitmap。 IWICBitmapFlipRotator是一個IWICBitmapSource,可用於創建圖像。
您需要創建一個IWICBitmapEncoder和IWICBitmapFrameEncode對象。
MSDN在這里有一個示例: https : //msdn.microsoft.com/zh-cn/library/windows/desktop/gg430021(v=vs.85).aspx
MSDN的示例期望您將手動提供圖像數據,但是您可以使用IWICBitmapFrameEncode :: WriteSource方法直接從IWICBitmapFlipRotator編寫: https ://msdn.microsoft.com/zh-cn/library/windows/desktop/ee690159 (v = vs.85).aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.