加载透明背景色 [英] Loading Transparent backcolor

查看:111
本文介绍了加载透明背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个小问题,至少我认为是这样。



我可以使用以下方法加载透明度的png文件:



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));





代码适用于没有任何白色的图像。但是我的图像有白色问题。



我知道它与RGBA(255,255,255,0)和RGBA(255,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.

推荐答案

我们使用RGB(128,128,128)作为遮罩颜色。它工作正常。因为它是一种令人讨厌的颜色。



只加载一次图像(如启动或第一次绘制调用) - 不适用于每个绘制周期。
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.

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永远不会像人们希望的那样直截了当。在您使用TransparentBlt之前,您需要设置要透明的颜色。这是您尝试使用的代码。

希望这有用。



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.


这篇关于加载透明背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆