Combine StretchBlt and TransparentBlt correctly, so a transparent raster map can be created correctly

INTRODUCTION AND RELATED INFORMATION:

I recently asked here at SO the question of scaling a bitmap, so it can preserve image quality:

The bitmap image loses quality when stretched / compressed against the background of the buttons .

I tried to use the suggestion made in the comment, use `StretchBlt, so I made a small demo program.

It improved the sharpness of bitmaps after I set the stretch mode to BLACKONWHITE .

I would like to try to make part of a bitmap image with a specific color - for example, black, transparent.

I used to use TransparentBlt, but I don't know how to do it now.

Problem:

To maintain image sharpness, I need StretchBlt it in the DC memory, with the BLACKONWHITE stretch BLACKONWHITE .

The problem is that I don’t know how Blt transparently go to the main DC window.

Here is the code snippet from the demo application:

  case WM_PAINT: { // main window DC hdc = BeginPaint(hWnd, &ps); // main window client rectangle RECT r; GetClientRect( hWnd, &r ); // memory DC for double buffering HDC MemDC = CreateCompatibleDC( hdc ); // fill it with test brush FillRect( MemDC, &r, (HBRUSH)GetStockObject( GRAY_BRUSH ) ); // select loaded bitmap into memory DC HBITMAP old = (HBITMAP)SelectObject( MemDC, bmp ); // get bitmaps dimensions BITMAP b; GetObject( bmp, sizeof(BITMAP), &b ); // needed to preserve bitmap sharpness SetStretchBltMode( hdc, BLACKONWHITE ); StretchBlt( hdc, 0, 0, r.right - r.left, r.bottom - r.top, MemDC, 0, 0, b.bmWidth, b.bmHeight, SRCCOPY ); /* TransparentBlt( ... ); call should go here, so I can make portion of the bitmap transparent, in order for the gray brush can be seen */ // cleanup SelectObject( MemDC, old ); DeleteDC(MemDC); EndPaint(hWnd, &ps); } return 0L; break; 

Question:

How to change the code above so that the bitmap can be transparent so that you can see the test brush?

Original image below.

enter image description here

I just need to use TransparentBlt( ..., RGB( 0, 0, 0 ) ); to make it transparent in black areas.

An example image showing the result:

enter image description here

MY EFFORTS:

Browsing over the Internet, I found only simple tutorials on double buffering.

I did not find anything like this, but to be honest, I am inexperienced in the WIN32 API, so I do not know how to correctly formulate the question in order to improve the search results.

If you need more information, ask for it, and I will supply it.

Omitted to keep the question short.

+7
c ++ winapi
source share
1 answer

You need to create a mask using certain raster operations to copy only the pixels defining the mask. http://www.winprog.org/tutorial/transparency.html

The following code is MFC, but you can easily retrieve and convert MFC objects to standard GDI operations. http://www.codeproject.com/Articles/703/Drawing-Transparent-Bitmap-with-ease-with-on-the-f

+1
source share

All Articles