3

我正在使用 缩小图像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);
4

1 回答 1

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();
于 2011-01-09T12:11:05.427 回答