StretchBlt和过滤alpha通道

时间:2011-01-07 12:12:54

标签: c++ winapi gdi+ stretchblt

我正在使用StretchBlt()缩放图片。

http://img684.imageshack.us/img684/2152/stretchblt.png

如您所见,目前看来我必须在质量过滤和透明度之间做出选择。有没有办法让两者兼得?这是我需要执行的唯一图像操作,因此我宁愿避免使用额外的库。

我的代码:

HDC srcDC = CreateCompatibleDC(NULL);
SelectObject(srcDC, *phbmp);

HDC destDC = CreateCompatibleDC(srcDC);
HBITMAP NewBMP = CreateCompatibleBitmap(srcDC,NewWidth,NewHeight);
SelectObject(destDC,NewBMP);

SetStretchBltMode(destDC,HALFTONE);
SetBrushOrgEx(destDC,0,0,NULL);
if (StretchBlt(destDC,0,0,NewWidth,NewHeight,srcDC,0,0,width,height,SRCCOPY) == TRUE)
{           
    DeleteObject(*phbmp);
    *phbmp = NewBMP;                
    hr = S_OK;
}
else
    DeleteObject(NewBMP);
DeleteDC(srcDC);
DeleteDC(destDC);

1 个答案:

答案 0 :(得分:2)

最终完全放弃了GDI。事实证明,正确的做法是当然IWICImagingFactory。最终代码:

IWICImagingFactory *pImgFac;
hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pImgFac));

IWICBitmap* NewBmp;
hr = pImgFac->CreateBitmapFromHBITMAP(*phbmp,0,WICBitmapUseAlpha,&NewBmp);

BITMAPINFO bmi = {};
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = NewWidth;
bmi.bmiHeader.biHeight = -NewHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;

BYTE *pBits;
HBITMAP hbmp = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&pBits, NULL, 0);
hr = hbmp ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{               
    IWICBitmapScaler* pIScaler;
    hr = pImgFac->CreateBitmapScaler(&pIScaler);
    hr = pIScaler->Initialize(NewBmp,NewWidth,NewHeight,WICBitmapInterpolationModeFant);

    WICRect rect = {0, 0, NewWidth, NewHeight};
    hr = pIScaler->CopyPixels(&rect, NewWidth * 4, NewWidth * NewHeight * 4, pBits);

    if (SUCCEEDED(hr))
        *phbmp = hbmp;
    else
        DeleteObject(hbmp);

    pIScaler->Release();
}
NewBmp->Release();
pImgFac->Release();