询问者
绘制bitmap图标四周有黑块,如何处理?

问题
-
大家好,
我创建了一个窗口并绘制了一张图片作为背景,然后又绘制了一个图标,但是图标周围有黑块不知道咋处理。我的代码贴出来:
hdc = ::BeginPaint(hWnd, &ps); s_hdcMem = CreateCompatibleDC(hdc); convert2bmp(hBitmap, L"c:\\windows\\web\\wallpaper\\windows\\img0.jpg"); hPrevBitmap = (HBITMAP)::SelectObject(s_hdcMem, hBitmap); ::GetObject(hBitmap, sizeof(s_bm), &s_bm); ::GetWindowRect(hWnd, &rect); GetMonitorRect(NULL, monitor_rect); ::SetStretchBltMode(hdc, COLORONCOLOR);//STRETCH_HALFTONE ::StretchBlt(hdc, 0, 0, rect.Width(), rect.Height(), s_hdcMem, 0, 0, s_bm.bmWidth, s_bm.bmHeight, SRCCOPY); ::SelectObject(s_hdcMem, hPrevBitmap); CreateDesktopItem(hWnd, items); ::GetClientRect(hWnd, rect); for (auto item : items) { hBitmap = item.second->icon_handle_; hPrevBitmap = (HBITMAP)::SelectObject(s_hdcMem, hBitmap); ::GetObject(hBitmap, sizeof(s_bm), &s_bm); ::SetStretchBltMode(hdc, COLORONCOLOR); ::StretchBlt(hdc, last_item_x, last_item_y, s_bm.bmWidth, s_bm.bmHeight, s_hdcMem, 0, 0, s_bm.bmWidth, s_bm.bmHeight, SRCCOPY); last_item_y += 128; ::SelectObject(s_hdcMem, hPrevBitmap); } ::EndPaint(hWnd, &ps);
不知道是不是代码哪里处理不对。。。
全部回复
-
大家好,
我创建了一个窗口并绘制了一张图片作为背景,然后又绘制了一个图标,但是图标周围有黑块不知道咋处理。我的代码贴出来:
hdc = ::BeginPaint(hWnd, &ps); s_hdcMem = CreateCompatibleDC(hdc); convert2bmp(hBitmap, L"c:\\windows\\web\\wallpaper\\windows\\img0.jpg"); hPrevBitmap = (HBITMAP)::SelectObject(s_hdcMem, hBitmap); ::GetObject(hBitmap, sizeof(s_bm), &s_bm); ::GetWindowRect(hWnd, &rect); GetMonitorRect(NULL, monitor_rect); ::SetStretchBltMode(hdc, COLORONCOLOR);//STRETCH_HALFTONE ::StretchBlt(hdc, 0, 0, rect.Width(), rect.Height(), s_hdcMem, 0, 0, s_bm.bmWidth, s_bm.bmHeight, SRCCOPY); ::SelectObject(s_hdcMem, hPrevBitmap); CreateDesktopItem(hWnd, items); ::GetClientRect(hWnd, rect); for (auto item : items) { hBitmap = item.second->icon_handle_; hPrevBitmap = (HBITMAP)::SelectObject(s_hdcMem, hBitmap); ::GetObject(hBitmap, sizeof(s_bm), &s_bm); ::SetStretchBltMode(hdc, COLORONCOLOR); ::StretchBlt(hdc, last_item_x, last_item_y, s_bm.bmWidth, s_bm.bmHeight, s_hdcMem, 0, 0, s_bm.bmWidth, s_bm.bmHeight, SRCCOPY); last_item_y += 128; ::SelectObject(s_hdcMem, hPrevBitmap); } ::EndPaint(hWnd, &ps);
不知道是不是代码哪里处理不对。。。
-
你好,
感谢你在这里发帖。
>>账户验证,插不了图
访问以下链接,留下评论,等待账户验证,随后你就可以在你的帖子里上传图片了。
Best Regards,
如果您对Visual Studio 或Microsoft Azure相关产品感兴趣,请点击此链接,或扫描以下二维码注册获取相关信息。
- 已编辑 Suarez-ZhouMicrosoft contingent staff 2019年11月28日 1:19
-
你好,
你的代码里似乎还有些自定义的函数,很多变量的类型也没有提供,很抱歉我们无法进行还原测试。
我猜想很大可能是与你绘制图标的方式有关,即循环调用SetStretchBltMode和StretchBlt那里,还有你似乎用了HBITMAP存放了icon图标信息,这也有点问题,当然也有可能跟icon文件本身有关。其他参数设置目前没发现有什么问题。
下面是我的demo,使用的最简单的GDI绘制,并没有发现与你相似的错误,仅供参考。
PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); HBITMAP hbmp = (HBITMAP)LoadImage(NULL, L"bmp文件路径", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); BITMAP bmpinfo = { 0 }; ::GetObject(hbmp, sizeof(bmpinfo), &bmpinfo); HDC s_hdcMem = CreateCompatibleDC(hdc); SelectObject(s_hdcMem, hbmp); BitBlt(hdc, 0, 0, bmpinfo.bmWidth, bmpinfo.bmHeight, s_hdcMem, 0, 0, SRCCOPY); HICON icon = (HICON)LoadImage(NULL, L"ico文件路径", IMAGE_ICON, 0, 0, LR_LOADFROMFILE); DrawIcon(hdc, 100, 100, icon); DeleteObject(hbmp); DeleteObject(icon); DeleteDC(s_hdcMem); EndPaint(hWnd, &ps);
Best Regards,
Suarez Zhou
如果您对Visual Studio 或Microsoft Azure相关产品感兴趣,请点击此链接,或扫描以下二维码注册获取相关信息。
- 已编辑 Suarez-ZhouMicrosoft contingent staff 2019年11月28日 3:13