int i(0);
int j(0);
CFileDialog dlg(TRUE,"bmp","*.bmp",OFN_HIDEREADONLY,"BMP文件");
dlg.DoModal();
CString str = dlg.GetPathName();
CBitmap bmp;
HBITMAP hbmp;
//将pStatic指向要显示图片的地方
CStatic *pStactic;
pStactic=(CStatic*)GetDlgItem(IDC_STATIC);//IDC_PHOTO为picture控件ID
// strpicname = "a.bmp";
//加载资源
hbmp=(HBITMAP)::LoadImage(NULL,str,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
bmp.Attach(hbmp);//获得加载图片的句柄
//获取图片格式
BITMAP bmInfo;
bmp.GetBitmap(&bmInfo);
//创建设备内存
CDC dcMem;
dcMem.CreateCompatibleDC(GetDC());
CBitmap *poldmap=(CBitmap*)dcMem.SelectObject(bmp);
//获得矩形区域   
CRect lRect;
pStactic->GetClientRect(&lRect);
//显示位图
pStactic->GetDC()->StretchBlt(lRect.left,lRect.top,lRect.Width(),lRect.Height(),&dcMem,0,0,bmInfo.bmWidth,bmInfo.bmHeight,SRCCOPY);
dcMem.SelectObject(&poldmap);
COLORREF color; //= new COLORREF[bmInfo.bmHeight * bmInfo.bmWidth];
int **red = new int*[bmInfo.bmWidth];
for ( i = 0; i < bmInfo.bmWidth ; i++)
{
red[i] = new int[bmInfo.bmHeight];
}
int **green = new int*[bmInfo.bmWidth];
for ( i = 0 ; i < bmInfo.bmWidth ; i ++)
{
green[i] = new int[bmInfo.bmHeight];
}
int **blue = new int*[bmInfo.bmWidth];
for ( i = 0 ; i < bmInfo.bmWidth ; i++)
{
blue[i] = new int[bmInfo.bmHeight];
}
for ( i = 0 ; i < bmInfo.bmWidth ; i++ )
{
for ( j = 0 ; j < bmInfo.bmHeight ; j++)
{
color = dcMem.GetPixel(i,j);
red[i][j] = GetRValue(color);
green[i][j] = GetGValue(color);
blue[i][j] = GetBValue(color);
red[i][j] = green[i][j] = blue[i][j] = ( red[i][j] + blue[i][j] + green[i][j] ) / 3;
color = RGB(red[i][j],green[i][j],blue[i][j] );
dcMem.SetPixel(bmInfo.bmWidth,bmInfo.bmHeight,color);
}

我想显示灰度化后的图片该怎么操作

解决方案 »

  1.   

    StretchBlt里面最后一个参数修改WHITENESS和BLASKNESS就可以达到效果的
      

  2.   

    你这种方法效率很低的.
    一个个像素点操作.最好用GDI+的矩阵.
      

  3.   

    给楼主一段简单实用的代码,用于显示一幅图片。CBitmap bmp;
    BITMAP bm;bmp.LoadBitmap(IDB_BITMAP1);
    bmp.GetBitmap(&bm);
    CDC *pDC=GetDC();
    CDC memdc;
    if(memdc.CreateCompatibleDC(pDC))
    {
        memdc.SelectObject(&bmp);
        pDC->BitBlt(0,0, bm.bmWidth, bm.bmHeight, &memdc, 0, 0, SRCCOPY);
    }
    ReleaseDC(pDC);至于如何显示这幅图片的回复,有兴趣的话继续提供代码。