!!!!!!!!!!!!!!

解决方案 »

  1.   

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

  2.   

    png图片处理成变灰的效果,咋办?png图片上半透明效果是不是就保不住了
      

  3.   

    RGB分量改成一样的,但是ALPHA分量不动,必须保存为32位的。
      

  4.   

    最好 事先 做好 一个 “带透明的 灰色 png 图片”
      

  5.   

    你的思路 有 问题。
    不能 直接 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 无关
      

  6.   

    要带 透明 则:
    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);
    }
    }
      

  7.   


    有一个优化效率的代码//判断出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;
    }
    }
    }
    }
    }
    }
      

  8.   

    对不对啊 我这调用这么代码显示不出来图案 不知道那的原因 这代码有没问题呢
    上面代码不处理背景图的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;
                            }
                        }
                    }
                }
            }
        }