| |

VerySource

 Forgot password?
 Register
Search
View: 608|Reply: 2

Related issues of BMP pictures turning transparent

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-7 12:10:02
| Show all posts |Read mode
Can I load a BMP picture in MFC to make its background color transparent?
Is it necessary to call what function to achieve
I know that BMP format pictures do not support transparency, but I think it should be possible to implement it with functions
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-8 00:36:01
| Show all posts
Give you a function of transparent bitmap

void TransparentBlt2 (HDC hdcDest, // target DC
  int nXOriginDest, // target X offset
  int nYOriginDest, // target Y offset
  int nWidthDest, // target width
  int nHeightDest, // target height
  HDC hdcSrc, // source DC
  int nXOriginSrc, // source X starting point
  int nYOriginSrc, // source Y starting point
  int nWidthSrc, // source width
  int nHeightSrc, // source height
  COLORREF crTransparent // transparent color, COLORREF type
  )
{
HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap (hdcDest, nWidthDest, nHeightDest); // create compatible bitmap
HBITMAP hOldMaskBMP, hMaskBMP = CreateBitmap (nWidthDest, nHeightDest, 1, 1, NULL); // create a monochrome mask bitmap
HDChImageDC = CreateCompatibleDC (hdcDest);
HDChMaskDC = CreateCompatibleDC (hdcDest);
hOldImageBMP = (HBITMAP) SelectObject (hImageDC, hImageBMP);
hOldMaskBMP = (HBITMAP) SelectObject (hMaskDC, hMaskBMP);

// Copy the bitmap from the source DC to the temporary DC
if (nWidthDest == nWidthSrc&&nHeightDest == nHeightSrc)
BitBlt (hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
else
StretchBlt (hImageDC, 0, 0, nWidthDest, nHeightDest,
hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);

// set transparent color
SetBkColor (hImageDC, crTransparent);

// Generate a mask bitmap with transparent areas in white and other areas in black
BitBlt (hMaskDC, 0, 0, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCCOPY);

// Generate a bitmap in which the transparent area is black and the other areas remain unchanged
SetBkColor (hImageDC, RGB (0,0,0));
SetTextColor (hImageDC, RGB (255,255,255));
BitBlt (hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);

// The transparent part keeps the screen unchanged, the other parts become black
SetBkColor (hdcDest, RGB (0xff, 0xff, 0xff));
SetTextColor (hdcDest, RGB (0,0,0));
BitBlt (hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);

// "OR" operation to generate the final effect
BitBlt (hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT);

SelectObject (hImageDC, hOldImageBMP);
DeleteDC (hImageDC);
SelectObject (hMaskDC, hOldMaskBMP);
DeleteDC (hMaskDC);
DeleteObject (hImageBMP);
DeleteObject (hMaskBMP);

}
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-8 01:00:01
| Show all posts
GDI has the following function, as long as you specify the transparent color
BOOL TransparentBlt (
  HDC hdcDest, // handle to destination DC
  int nXOriginDest, // x-coord of destination upper-left corner
  int nYOriginDest, // y-coord of destination upper-left corner
  int nWidthDest, // width of destination rectangle
  int hHeightDest, // height of destination rectangle
  HDC hdcSrc, // handle to source DC
  int nXOriginSrc, // x-coord of source upper-left corner
  int nYOriginSrc, // y-coord of source upper-left corner
  int nWidthSrc, // width of source rectangle
  int nHeightSrc, // height of source rectangle
  UINT crTransparent // color to make transparent
);
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list