Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello Everyone,

I am having a small issue at least that's what I think it is.

I can load a png file with transparency using the following:

CImage Image;
////More code
///
Image.TransparentBlt(dc.m_hdc,0,0,20,20, RGB(255,255,255));


The code works on images which don't have any white. However I am having issues with images that have white.

I know its something to do with RGBA(255,255,255,0) and RGBA(255,255,255,255).
However I can't seem to resolve it. Looked and searched but I either missed or didn't understand it.

Can someone please assist me in resolving this issue.

Thanks in advance.
Posted
Comments
The_Inventor 17-Feb-14 22:50pm    
RGBA = Red, Green, Blue, Alpha. You loaded the image as a RGB and not RGBA. The Alpha channel is what controls the transparency. The 1st pixel, usually at 0,0 is use as the color that is going to be 'transparent'. In Hollywood they use Green, but typically the pixel is usually white=RGB(255,255,255) or Black=RGB(0,0,0). When using RGBA(0,0,0,0) you get all black. If you use RGBA(0,0,0,255), what is black is transparent. If you use RGBA(255,255,255,0) you get all solid white. If you use RGBA(255,255,255,255) then what is white is transparent.
FISH786 18-Feb-14 4:54am    
Thank you. The question I would like to ask now is how load the image for RGBA?
The_Inventor 18-Feb-14 20:23pm    
Image.TransparentBlt(dc.m_hdc,0,0,20,20, RGBA(255,255,255,0));
FISH786 19-Feb-14 7:59am    
I tried doing this. I got a compile error.
RGBA: Identifier not found.

We use the RGB(128,128,128) as mask color. It works fine. because it is a nasty color.

Load the image only once (like startup or first draw call) - not for every draw cycle.
 
Share this answer
 
C++
void CChildView::OnFileOpenimage(void)
{
	//CImage imgOrginal; //from the header.
	CString strFilter;
	CSimpleArray<guid> aguidFileTypes;
	HRESULT hResult;

	hResult = imgOriginal.GetExporterFilterString(strFilter,aguidFileTypes);
	if (FAILED(hResult)) {
		CString fmt;
		fmt.Format("GetExporterFilter failed:\n%x - %s", hResult, _com_error(hResult).ErrorMessage());
		::AfxMessageBox(fmt);
		return;
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	if (!imgOriginal.IsNull()) 
	{

		switch (m_nImageSize)
		{
			case SIZE_HALF:
			imgOriginal.StretchBlt(dc,0,0,imgOriginal.GetWidth()/2,imgOriginal.GetHeight()/2,SRCCOPY);
				break;
			case SIZE_ORIGINAL:
			imgOriginal.StretchBlt(dc,0,0,imgOriginal.GetWidth(),imgOriginal.GetHeight(),SRCCOPY);
				break;
			case SIZE_DOUBLE:
			imgOriginal.StretchBlt(dc,0,0,imgOriginal.GetWidth()*2,imgOriginal.GetHeight()*2,SRCCOPY);
				break;
			case SIZE_FILL:
			CRect rctWindowSize;
			GetClientRect(rctWindowSize);
			imgOriginal.StretchBlt(dc,0,0,rctWindowSize.Width(),rctWindowSize.Height(),SRCCOPY);
		};
	}

}
/////////////////////////////////
From atlimage.h
void SetHasAlphaChannel( bool bHasAlphaChannel ) throw();
	LONG SetTransparentColor( LONG iTransparentColor ) throw();
	COLORREF SetTransparentColor( COLORREF clrTransparentColor ) throw();

	BOOL TransparentBlt( HDC hDestDC, int xDest, int yDest, int nDestWidth, 
		int nDestHeight, UINT crTransparent = CLR_INVALID ) const throw();

	BOOL TransparentBlt( HDC hDestDC, const RECT& rectDest, 
		UINT crTransparent = CLR_INVALID ) const throw();

	BOOL TransparentBlt( HDC hDestDC, int xDest, int yDest, int nDestWidth,
		int nDestHeight, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight,
		UINT crTransparent = CLR_INVALID ) const throw();

	BOOL TransparentBlt( HDC hDestDC, const RECT& rectDest, const RECT& rectSrc,
		UINT crTransparent = CLR_INVALID ) const throw();
	
        COLORREF GetTransparentRGB() const;

inline BOOL CImage::TransparentBlt( HDC hDestDC, int xDest, int yDest, 
	int nDestWidth, int nDestHeight, int xSrc, int ySrc, int nSrcWidth, 
	int nSrcHeight, UINT crTransparent ) const throw()
{
	BOOL bResult;

	ATLASSUME( m_hBitmap != NULL );
	ATLENSURE_RETURN_VAL( hDestDC != NULL, FALSE );

	GetDC();

	if( crTransparent == CLR_INVALID )
	{
		crTransparent = GetTransparentRGB();
	}

	bResult = ::TransparentBlt( hDestDC, xDest, yDest, nDestWidth, nDestHeight,
		m_hDC, xSrc, ySrc, nSrcWidth, nSrcHeight, crTransparent );

	ReleaseDC();

	return( bResult );
}



</guid>


It is never as straight forward as one might hope. Before you TransparentBlt you need to set the color that isgoing to be transparent. This is the code that you are trying to use.
Hope this helpd.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900