DrawIconEx leaving mask artifacts

2019-05-04 08:43发布

问题:

I'm extracting jumbo icons for any given path using IImageList and SHGetFileInfo. Once I have that, I then render the HICON into a HBITMAP using DrawIconEx for eventual rendering with GDI+ Bitmap and Graphics objects.

Now, this all works great, except that when I do the final rendering of the bitmap, the very left edge always has a black artifact on it. This is true for pretty much any icon I get, and is always the left edge.

Any ideas where the dark line could be coming from?

The code I'm using is roughly:

1. Extract Icon:

// Get the image list index of the icon
SHFILEINFO sfi;
if (!SHGetFileInfo(pszPath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX)) return NULL;

// Get the jumbo image list
IImageList *piml;
if (FAILED(SHGetImageList(SHIL_JUMBO, IID_PPV_ARGS(&piml)))) return NULL;

// Extract an icon
HICON hicon;
piml->GetIcon(sfi.iIcon, ILD_SCALE|ILD_TRANSPARENT, &hicon);
return hicon;

2. Generate Bitmap

HDC hDC = GetDC(NULL);
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, x, y);
HBITMAP hResultBmp = NULL;
HGDIOBJ hOrgBMP = SelectObject(hMemDC, hMemBmp);

HBRUSH hbr = CreateSolidBrush(bg);

RECT rr = { 0, 0, 256, 256 }; // jumbo icons
FillRect(hMemDC, &rr, hbr);
DeleteBrush(hbr);
DrawIconEx(hMemDC, 0, 0, hicon, size, size, 0, NULL, DI_NORMAL);

hResultBmp = hMemBmp;
hMemBmp = NULL;

SelectObject(hMemDC, hOrgBMP);
return hResultBitmap;

3. Render GDI+ Bitmap to "window bitmap":

Bitmap *b = ::New Bitmap(hResultBitmap, NULL);

Graphics    graphics(hdc);
graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);

SolidBrush  bgbrush(Color(255, 255, 255, 255));
Rect r(0, 0, hwnd_w, hwnd_h);
graphics.FillRectangle(&bgbrush, r);

graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
Rect r(5, 5, 128, 128);
graphics.DrawImage(dpd->image_to_draw, r);

回答1:

Wow, I spent another while last night playing with it. It's the ILD_SCALE in IImageList::GetIcon.

Get rid of that and it all works perfectly fine again. Go figure …

1. Extract Icon:

// Get the image list index of the icon
SHFILEINFO sfi;
if (!SHGetFileInfo(pszPath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX)) return NULL;

// Get the jumbo image list
IImageList *piml;
if (FAILED(SHGetImageList(SHIL_JUMBO, IID_PPV_ARGS(&piml)))) return NULL;

// Extract an icon
HICON hicon;
piml->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hicon);
return hicon;