png图片变灰,啥算法如何处理?//透明效果要保存呢 !!!!!!!!!!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 for(int i=0;i<bitmap.bmWidth;i++) for(int j=0;j<bitmap.bmHeight;j++) { pixelcolor = dc1.GetPixel(i,j); gray = (BYTE)(GetRValue(pixelcolor)*0.2+GetGValue(pixelcolor)*0.4+GetBValue(pixelcolor)*0.2); dc1.SetPixel(i,j,RGB(gray,gray,gray)); } png图片处理成变灰的效果,咋办?png图片上半透明效果是不是就保不住了 RGB分量改成一样的,但是ALPHA分量不动,必须保存为32位的。 最好 事先 做好 一个 “带透明的 灰色 png 图片” 你的思路 有 问题。不能 直接 DC SetPixel, 还 应该 在 Bitmap 中 处理:void CDlgBase::OnPaint(){ GetUpdateRect(&m_rcUpdate, FALSE);// do not delete this CPaintDC dc(this); dc.SetBkMode(TRANSPARENT); CRect rcClient; GetClientRect(rcClient);#if 1 Bitmap *bmp = new Bitmap(L"bk.bmp"); int wid=bmp->GetWidth(); int hei=bmp->GetHeight(); afxDump << wid << ";" << hei << "\n";// 480,800// color to grey for(int i=0;i<wid;i++) { for(int j=0;j<hei;j++) { Gdiplus::Color pix; bmp->GetPixel(i,j,&pix); BYTE gray = pix.GetRed()*0.2+pix.GetGreen()*0.4+pix.GetBlue()*0.2; bmp->SetPixel(i,j,RGB(gray,gray,gray)); } } TextureBrush *brush = new TextureBrush(bmp); Graphics graphics(dc); graphics.FillRectangle(brush, (Gdiplus::Rect &) rcClient); return ;//#endif注意那个 dc 与 setpixel 无关 要带 透明 则:bmp->SetPixel(i,j,RGB(gray,gray,gray));// a 丢了 !需改为: for(int i=0;i<wid;i++) { for(int j=0;j<hei;j++) { Gdiplus::Color pix; bmp->GetPixel(i,j,&pix); BYTE gray = pix.GetRed()*0.2+pix.GetGreen()*0.4+pix.GetBlue()*0.2; ARGB argb=(pix.GetA() << 24) | (gray << 16) | (gray << 8) | gray; bmp->SetPixel(i,j,argb); } } 有一个优化效率的代码//判断出32位png,你看我处理的对吗(按照你上面的代码改的) !!!还有背景问题!!!另外一个问题 Image类有GetWidth GetHeight函数获取png图片的宽与高,直接循环遍历png图片不行吗???BITMAP bmDis; if(::GetObject((HBITMAP)imageState[1]->BitSave.m_hObject,sizeof(BITMAP),&bmDis)) //获取位图!!! { if (bmDis.bmBits != 0 && (bmDis.bmBitsPixel == 24 || bmDis.bmBitsPixel == 32)) { BYTE *byteptr = (BYTE *)bmDis.bmBits; int lineadd = bmDis.bmWidthBytes - bmDis.bmWidth * (bmDis.bmBitsPixel >> 3); if (bmDis.bmBitsPixel >> 3 == 3) //24 Bit BMP { for (int i = bmDis.bmHeight; i > 0; i--) { for (int j = bmDis.bmWidth; j > 0; j--) { if((*byteptr!= 255)&& (*(byteptr+1) != 0)&&(*(byteptr+2) != 255))//不是背景色255,0,255进行变灰处理!!! { *byteptr = *(byteptr + 1) = *(byteptr + 2) = (BYTE)(*(byteptr + 2) * 0.2 + *(byteptr + 1) * 0.4 + *byteptr * 0.2); byteptr += 3; } } byteptr += lineadd; } } else //32 Bit Png 全图是alpha通道125 。但是,只是前景图片进行变灰处理。 { //Gdiplus::Color pix; for (int i = bmDis.bmHeight; i > 0; i--) { for (int j = bmDis.bmWidth; j > 0; j--) { //不处理背景图。 if((*byteptr!= 255)&& (*(byteptr+1)!= 0)&&(*(byteptr+2)!= 255))//这个需要加alpha通道处理吗!背景颜色不是255,0,255,有一个alpha 125通道呀 { BYTE gray = *byteptr = *(byteptr + 1) = *(byteptr + 2) = (BYTE)(*(byteptr + 2) * 0.2 + *(byteptr + 1) * 0.4 + *byteptr * 0.2); ARGB argb = *(byteptr + 3)<<24 | gray<<16 | gray<< 8 | gray; *byteptr = *(byteptr + 1) = *(byteptr + 2) = *(byteptr + 3) = argb; byteptr += 4; } } } } } } 对不对啊 我这调用这么代码显示不出来图案 不知道那的原因 这代码有没问题呢上面代码不处理背景图的if判断没有alpha通道呀,png不需要啊??? //不处理背景图。 if((*byteptr!= 255)&& (*(byteptr+1)!= 0)&&(*(byteptr+2)!= 255)&&(*(byteptr+3)!=125))//这个需要加alpha通道处理吗!背景颜色不是255,0,255,有一个alpha 125通道呀BITMAP bmDis; if(::GetObject((HBITMAP)imageState[1]->BitSave.m_hObject,sizeof(BITMAP),&bmDis)) //获取位图!!! { if (bmDis.bmBits != 0 && (bmDis.bmBitsPixel == 24 || bmDis.bmBitsPixel == 32)) { BYTE *byteptr = (BYTE *)bmDis.bmBits; int lineadd = bmDis.bmWidthBytes - bmDis.bmWidth * (bmDis.bmBitsPixel >> 3); if (bmDis.bmBitsPixel >> 3 == 3) //24 Bit BMP { for (int i = bmDis.bmHeight; i > 0; i--) { for (int j = bmDis.bmWidth; j > 0; j--) { if((*byteptr!= 255)&& (*(byteptr+1) != 0)&&(*(byteptr+2) != 255))//不是背景色255,0,255进行变灰处理!!! { *byteptr = *(byteptr + 1) = *(byteptr + 2) = (BYTE)(*(byteptr + 2) * 0.2 + *(byteptr + 1) * 0.4 + *byteptr * 0.2); byteptr += 3; } } byteptr += lineadd; } } else //32 Bit Png 全图是alpha通道125 。但是,只是前景图片进行变灰处理。 { //Gdiplus::Color pix; for (int i = bmDis.bmHeight; i > 0; i--) { for (int j = bmDis.bmWidth; j > 0; j--) { //不处理背景图。 if((*byteptr!= 255)&& (*(byteptr+1)!= 0)&&(*(byteptr+2)!= 255)&&(*(byteptr+3)!=125))//这个需要加alpha通道处理吗!背景颜色不是255,0,255,有一个alpha 125通道呀 { BYTE gray = *byteptr = *(byteptr + 1) = *(byteptr + 2) = (BYTE)(*(byteptr + 2) * 0.2 + *(byteptr + 1) * 0.4 + *byteptr * 0.2); ARGB argb = *(byteptr + 3)<<24 | gray<<16 | gray<< 8 | gray; *byteptr = *(byteptr + 1) = *(byteptr + 2) = *(byteptr + 3) = argb; byteptr += 4; } } } } } } ADO查询ORACLE sys_guid返回乱码 为什么我打开的位图文件总是掉色呢? 网络编程的重叠IO问题 简单:friend关键字 如何将剪贴板中的位图绘制到一个内存DC上? visua c++中的动画如何添加 关于opengles中egl的问题 如何在CRichEditView中正确的显示中文 AutoCAD二次开发的程序员请帮忙,急!!急!!急!! 你们都用的什么数据库啊 程序在任务管理器中没有显示 C++菜鸟,问下一个程序问题
for(int j=0;j<bitmap.bmHeight;j++)
{
pixelcolor = dc1.GetPixel(i,j);
gray = (BYTE)(GetRValue(pixelcolor)*0.2+GetGValue(pixelcolor)*0.4+GetBValue(pixelcolor)*0.2);
dc1.SetPixel(i,j,RGB(gray,gray,gray));
}
不能 直接 DC SetPixel, 还 应该 在 Bitmap 中 处理:void CDlgBase::OnPaint()
{
GetUpdateRect(&m_rcUpdate, FALSE);// do not delete this
CPaintDC dc(this);
dc.SetBkMode(TRANSPARENT);
CRect rcClient;
GetClientRect(rcClient);
#if 1
Bitmap *bmp = new Bitmap(L"bk.bmp");
int wid=bmp->GetWidth();
int hei=bmp->GetHeight();
afxDump << wid << ";" << hei << "\n";// 480,800
// color to grey
for(int i=0;i<wid;i++)
{
for(int j=0;j<hei;j++)
{
Gdiplus::Color pix;
bmp->GetPixel(i,j,&pix);
BYTE gray = pix.GetRed()*0.2+pix.GetGreen()*0.4+pix.GetBlue()*0.2;
bmp->SetPixel(i,j,RGB(gray,gray,gray));
}
}
TextureBrush *brush = new TextureBrush(bmp);
Graphics graphics(dc);
graphics.FillRectangle(brush, (Gdiplus::Rect &) rcClient);
return ;
//
#endif
注意那个 dc 与 setpixel 无关
bmp->SetPixel(i,j,RGB(gray,gray,gray));// a 丢了 !
需改为:
for(int i=0;i<wid;i++)
{
for(int j=0;j<hei;j++)
{
Gdiplus::Color pix;
bmp->GetPixel(i,j,&pix);
BYTE gray = pix.GetRed()*0.2+pix.GetGreen()*0.4+pix.GetBlue()*0.2;
ARGB argb=(pix.GetA() << 24) | (gray << 16) | (gray << 8) | gray;
bmp->SetPixel(i,j,argb);
}
}
有一个优化效率的代码//判断出32位png,你看我处理的对吗(按照你上面的代码改的) !!!还有背景问题!!!
另外一个问题 Image类有GetWidth GetHeight函数获取png图片的宽与高,直接循环遍历png图片不行吗???
BITMAP bmDis;
if(::GetObject((HBITMAP)imageState[1]->BitSave.m_hObject,sizeof(BITMAP),&bmDis))
//获取位图!!!
{
if (bmDis.bmBits != 0 && (bmDis.bmBitsPixel == 24 || bmDis.bmBitsPixel == 32))
{
BYTE *byteptr = (BYTE *)bmDis.bmBits;
int lineadd = bmDis.bmWidthBytes - bmDis.bmWidth * (bmDis.bmBitsPixel >> 3); if (bmDis.bmBitsPixel >> 3 == 3) //24 Bit BMP
{
for (int i = bmDis.bmHeight; i > 0; i--)
{
for (int j = bmDis.bmWidth; j > 0; j--)
{
if((*byteptr!= 255)&& (*(byteptr+1) != 0)&&(*(byteptr+2) != 255))//不是背景色255,0,255进行变灰处理!!!
{
*byteptr = *(byteptr + 1) = *(byteptr + 2) = (BYTE)(*(byteptr + 2) * 0.2 + *(byteptr + 1) * 0.4 + *byteptr * 0.2);
byteptr += 3;
}
}
byteptr += lineadd;
}
}
else //32 Bit Png 全图是alpha通道125 。但是,只是前景图片进行变灰处理。
{
//Gdiplus::Color pix;
for (int i = bmDis.bmHeight; i > 0; i--)
{
for (int j = bmDis.bmWidth; j > 0; j--)
{
//不处理背景图。
if((*byteptr!= 255)&& (*(byteptr+1)!= 0)&&(*(byteptr+2)!= 255))//这个需要加alpha通道处理吗!背景颜色不是255,0,255,有一个alpha 125通道呀
{
BYTE gray = *byteptr = *(byteptr + 1) = *(byteptr + 2) = (BYTE)(*(byteptr + 2) * 0.2 + *(byteptr + 1) * 0.4 + *byteptr * 0.2);
ARGB argb = *(byteptr + 3)<<24 | gray<<16 | gray<< 8 | gray;
*byteptr = *(byteptr + 1) = *(byteptr + 2) = *(byteptr + 3) = argb;
byteptr += 4;
}
}
}
}
}
}
上面代码不处理背景图的if判断没有alpha通道呀,png不需要啊??? //不处理背景图。
if((*byteptr!= 255)&& (*(byteptr+1)!= 0)&&(*(byteptr+2)!= 255)&&(*(byteptr+3)!=125))//这个需要加alpha通道处理吗!背景颜色不是255,0,255,有一个alpha 125通道呀
BITMAP bmDis;
if(::GetObject((HBITMAP)imageState[1]->BitSave.m_hObject,sizeof(BITMAP),&bmDis))
//获取位图!!!
{
if (bmDis.bmBits != 0 && (bmDis.bmBitsPixel == 24 || bmDis.bmBitsPixel == 32))
{
BYTE *byteptr = (BYTE *)bmDis.bmBits;
int lineadd = bmDis.bmWidthBytes - bmDis.bmWidth * (bmDis.bmBitsPixel >> 3);
if (bmDis.bmBitsPixel >> 3 == 3) //24 Bit BMP
{
for (int i = bmDis.bmHeight; i > 0; i--)
{
for (int j = bmDis.bmWidth; j > 0; j--)
{
if((*byteptr!= 255)&& (*(byteptr+1) != 0)&&(*(byteptr+2) != 255))//不是背景色255,0,255进行变灰处理!!!
{
*byteptr = *(byteptr + 1) = *(byteptr + 2) = (BYTE)(*(byteptr + 2) * 0.2 + *(byteptr + 1) * 0.4 + *byteptr * 0.2);
byteptr += 3;
}
}
byteptr += lineadd;
}
}
else //32 Bit Png 全图是alpha通道125 。但是,只是前景图片进行变灰处理。
{
//Gdiplus::Color pix;
for (int i = bmDis.bmHeight; i > 0; i--)
{
for (int j = bmDis.bmWidth; j > 0; j--)
{
//不处理背景图。
if((*byteptr!= 255)&& (*(byteptr+1)!= 0)&&(*(byteptr+2)!= 255)&&(*(byteptr+3)!=125))//这个需要加alpha通道处理吗!背景颜色不是255,0,255,有一个alpha 125通道呀
{
BYTE gray = *byteptr = *(byteptr + 1) = *(byteptr + 2) = (BYTE)(*(byteptr + 2) * 0.2 + *(byteptr + 1) * 0.4 + *byteptr * 0.2);
ARGB argb = *(byteptr + 3)<<24 | gray<<16 | gray<< 8 | gray;
*byteptr = *(byteptr + 1) = *(byteptr + 2) = *(byteptr + 3) = argb;
byteptr += 4;
}
}
}
}
}
}