突然想将Windows 2d游戏中现成的游戏资源转换为Cocos2d中可用的Png图片 但老的windows游戏中都不采用带有Alpha通道的bitmap图片
因此在BitBlt中要用两个图片实现透明效果 SRCAND SRCPAINT做与作或运算
1.用Ps将图片转成png在将第一张的黑色删去 但这会让一些不该透明的也透明 很可能一片模糊(|--)
2我采用的。。。。
就是用GDI+ 中的CImage 只要在将与或运算在函数里合成一张图片之后用ps色彩范围就可以完美实现
void TwoGifBeOnePng(CString source,CString dest,CString dest2,CString filename)
{
CString str = filename;/*"D:\\block-8m.gif"*/
if(str.Right(5) == "m.gif")
return;
if(source.Right(1) != "\\");
source += "\\";
if(dest.Right(1) != "\\");
dest += "\\";
if(dest2.Right(1) != "\\");
dest2 += "\\";
CString strTemp = str.Left(str.GetLength() - 4);
CString str1 = str;
CString str2 = strTemp + "m.gif";
CImage imgOr;
imgOr.Load(source + str1);
if(FALSE == PathFileExists(source + str2))
{
imgOr.Save(dest2 + strTemp + ".png",ImageFormatPNG);
return;
}
CImage imgAnd;
imgAnd.Load(source + str2);
//创建一张白的CImage
CImage imgAnother;
imgAnother.Create(imgAnd.GetWidth(),imgAnd.GetHeight(),32,0);
CDC* pDC = CDC::FromHandle( imgAnother.GetDC());
pDC ->SelectObject( CBrush(RGB(255,255,255)));
pDC ->SelectObject(CPen(PS_NULL,1,RGB(0,0,0)));
pDC ->Rectangle(0,0,imgAnother.GetWidth() +1,imgAnother.GetHeight()+1);
//创建一张空的Image
CImage imgPng;
imgPng.Create(imgAnd.GetWidth(),imgAnd.GetHeight(),32,0);
HDC hDC=imgPng.GetDC();
//将白色 屏蔽图 原图 分别放入 新图
imgAnother.BitBlt(hDC,0,0,SRCCOPY);
imgAnother.ReleaseDC();
imgAnd.BitBlt(hDC,0,0,SRCAND);
imgOr.BitBlt(hDC,0,0,SRCPAINT);
//保存
imgPng.Save(dest + strTemp + ".png",ImageFormatPNG);
imgAnd.Detach();
imgOr.Detach();
imgPng.ReleaseDC();
imgPng.Detach();
}
这里原图比屏蔽图少了一个m 如1.gif 1m.gif
之后入股你想批处理 一个文件夹下所有资源
void TwoGifBeOnePngs(string Source,string Dest1,string Dest2)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH] = {0};
strncpy(DirSpec, Source.c_str(),Source.length());
DWORD dwError;
char realDir[MAX_PATH] = {0};
strncat(DirSpec,realDir,strlen(realDir) + 1);
printf ("Target directory is %s.\n", DirSpec);
strncat (DirSpec, "\\", 2);
char Pre[MAX_PATH] = {0};
strncat(Pre,DirSpec,strlen(DirSpec) + 1);
strncat (DirSpec, "*", 2);
hFind = FindFirstFile(DirSpec, &FindFileData);
int n = 0;
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid file handle. Error is %u\n", GetLastError());
return ;
}
else
{
do
{
if(FALSE == PathFileExists(Dest1.c_str()))
CreateDirectory(Dest1.c_str(),NULL);
if(FALSE == PathFileExists(Dest2.c_str()))
CreateDirectory(Dest2.c_str(),NULL);
TwoGifBeOnePng(CString(Pre) ,Dest1.c_str(),Dest2.c_str(),FindFileData.cFileName);
n++;
}while(FindNextFile(hFind, &FindFileData) != 0) ;
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("FindNextFile error. Error is %u\n", dwError);
return ;
}
else
{
printf ("You Successs Process File %d" ,n);
}
}
}
因为有的图片没有屏蔽图不想再Ps中处理另用的一个文件夹Dest2
之后之后打卡PS用选择色彩范围将容差调为零在批处理
效果还不错吧 之后整个上百张图游戏资源几分钟就转换为透明的PNG图片了