开发者

StretchBlt and filtering alpha channels

I am scaling an image down with StretchBlt().

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

As you can see, it currently looks like I have to choose betw开发者_Go百科een quality filtering and transparency. Is there any way to get both? This is the only image operation I need to perform, so I'd prefer to avoid extra libraries.

My code:

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


Gave up entirely on GDI in the end. Turns out that the proper way of doing this is, of course, with IWICImagingFactory. Final code:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜