Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine StretchBlt and TransparentBlt properly, so transparent bitmap can be created properly

Tags:

c++

winapi

INTRODUCTION AND RELEVANT INFORMATION:

Recently, I have asked, here in SO, a question about scaling a bitmap properly, so it can keep the quality of the picture:

Bitmap loses quality when stretched/shrinked on buttons background.

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

It did improve the bitmaps sharpness, after I have set stretch mode to BLACKONWHITE.

I would like to try to make the portion of the bitmap, with the certain color-say black for example, transparent.

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

PROBLEM:

In order to preserve the sharpness of the picture, I need to StretchBlt it in the memory DC, with stretch mode being BLACKONWHITE.

The problem is that I do not know how to Blt it transparently into main window's DC.

Here is a code snippet from the demo app:

    case WM_PAINT:
         {
              // main window's DC

              hdc = BeginPaint(hWnd, &ps);

              // main window's 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's 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 modify the above code, so a bitmap can be transparent, in order for test brush to be seen ?

The original image is bellow.

enter image description here

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

The example picture that shows result:

enter image description here

MY EFFORTS:

Browsing through Internet, I have found only simple tutorials, regarding double buffering.

I haven't found anything like this, but to be honest, I am inexperienced in WIN32 API, so I don't know how to phrase the question properly, in order to get better search results.

If further information is required, ask for it and I will supply it.

It is omitted to keep the question short.

like image 417
AlwaysLearningNewStuff Avatar asked Sep 22 '13 22:09

AlwaysLearningNewStuff


1 Answers

You Need to create a mask use specific raster operations to copy only the Pixels were the mask is defined. http://www.winprog.org/tutorial/transparency.html

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

like image 119
xMRi Avatar answered Nov 04 '22 04:11

xMRi