2011-01-07 72 views
3

我使用StretchBlt()缩放图像。StretchBlt和过滤alpha通道

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

您标记了GDI +,但您没有使用其优秀的调整大小过滤器?耻辱。 – 2011-01-07 12:30:55

回答

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