Click here to Skip to main content
15,886,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm converting a bitmap image into grey scale using MFC but got some errors kindly help. Here is my code:

C++
void CFingerprintSystem_FASSDlg::OnNormalization()
{
// TODO: Add your control notification handler code here

CString m_sBitmap;

HBITMAP hGrayBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),
m_sBitmap, IMAGE_BITMAP, 100,140, LR_LOADFROMFILE |
LR_CREATEDIBSECTION);

BITMAP bm;

GetObject(hGrayBitmap, sizeof(BITMAP), (LPSTR)&bm);

typedef struct tagBITMAP
{
LONG bmType;
LONG bmWidth;
LONG bmHeight;
LONG bmWidthBytes;
WORD bmPlanes;
WORD bmBitsPixel;
LPVOID bmBits;
}
BITMAP, *PBITMAP;


if (hGrayBitmap)
{
CStatic *pImg = (CStatic*)GetDlgItem(IDC_STATIC_Processed);
BYTE * pImgByte = (BYTE *) bm.bmBits;



if(pImg==NULL)
{
AfxMessageBox("Fail to convert grayscale");
}
else
{

INT iWidthBytes = bm.bmWidth * 3;

for ( int y = 0; y < bm.bmHeight; y++)
{
for ( int x = 0; x < bm.bmWidth*3; x++)
{

unsigned char R = pImgByte[y*iWidthBytes+x+2];
unsigned char G = pImgByte[y*iWidthBytes+x+1];
unsigned char B = pImgByte[y*iWidthBytes+x];;

INT gray = ceil(0.3*R + 0.59*G + 0.11*B);

pImgByte[y*iWidthBytes+x+2] = gray;
pImgByte[y*iWidthBytes+x+1] = gray;
pImgByte[y*iWidthBytes+x] = gray;
}
}

pImg->SetBitmap(hGrayBitmap);

}
}
}


There are errors
error C2665: 'AfxMessageBox' : none of the 2 overloads could convert all the argument types
        d:\visual studio\vc\atlmfc\include\afxwin.h(5372): could be 'int AfxMessageBox(LPCTSTR,UINT,UINT)'
        d:\visual studio\vc\atlmfc\include\afxwin.h(5374): or       'int AfxMessageBox(UINT,UINT,UINT)'
        while trying to match the argument list '(const char [26])'
Posted
Updated 7-Jun-10 22:59pm
v2

Try this:

C++
void CdrawbitmapDlg::OnBnClickedButton1()
{
   CBitmap img;
   CDC dc;
   BITMAP bmp;
   img.LoadBitmapW(IDB_BITMAP1);
   img.GetBitmap(&bmp);
   CDC* pDC = this->GetDC();
   dc.CreateCompatibleDC(pDC);
   CBitmap* pOld = dc.SelectObject(&img);
   for(int y = 0; y < bmp.bmHeight; y++)
   {
      for(int x = 0; x < bmp.bmWidth; x++)
      {
         COLORREF rgb = dc.GetPixel(x, y);
         BYTE r = GetRValue(rgb);
         BYTE g = GetGValue(rgb);
         BYTE b = GetBValue(rgb);
         BYTE bw = (BYTE)(0.3 * r + 0.59 * g + 0.11 * b + 0.5);
         dc.SetPixel(x, y, RGB(bw, bw, bw));
      }
   }
   pDC->BitBlt(100, 100, bmp.bmWidth, bmp.bmHeight, &dc, 0, 0, SRCCOPY);
   dc.SelectObject(pOld);
}
 
Share this answer
 
Comments
Sweety Khan 8-Jun-10 6:39am    
oh superb! its absolutely working perfectly....
I have no words to express how much i m happy n thankful to u......
thnx a lot. u r sooo sweet...

just tell me about these so tht i can easily answer in viva
1. is GetRValue(rgb) is only for getting RGB values?
2. explain the parameters of setpixel?
3. why u use dc with setpixel?
4. what is pOld?

again thnxxxx a lot. May ur problems n confusion will also solve vry easily...AMEEN
You are passing an ANSI string to he AfxMessageBox function, but that function require an ANSI or UNICODE string depending on your project settings.

Simply change the line:

C++
AfxMessageBox("Fail to convert grayscale");


to:

C++
AfxMessageBox(_T("Fail to convert grayscale"));


Note that the macro _T(x) gets a string and, if your project use the MBCS charset lets the string unmodified otherwise, if your project use the UNICODE charset the macro add a L before the string (e.g. L"my string") that means wide-char string
 
Share this answer
 
v2
Comments
Sweety Khan 8-Jun-10 2:38am    
thank u so much. there r two more error

error C2065: 'IDC_STATIC_Processed' : undeclared identifier
error C3861: 'ceil': identifier not found

waiting for ur reply
error C3861: 'ceil': identifier not found --> add #include <math.h> into your stdafx.h or on top of your .cpp file
error C2065: 'IDC_STATIC_Processed': undeclared identifier --> ensure that the identifier is correct, add #include "resource.h" on top of your .cpp file
 
Share this answer
 
v2
Comments
Sweety Khan 8-Jun-10 3:29am    
i m getting this error
fatal error C1189: #error : "include 'stdafx.h' before including this file for PCH"
Simply the #include "stdafx.h" must be placed before all other #include directives
 
Share this answer
 
Comments
Sweety Khan 8-Jun-10 4:27am    
i have done this

add #include <math.h> into your stdafx.h or on top of your .cpp file
undeclared identifier --> ensure that the identifier is correct, add #include "resource.h" on top of your .cpp file

but still getting the same errors :-(


can we convert bitmap image into grey scale by adding some code into the following code

void CdrawbitmapDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CRect r;
CBitmap* m_bitmap;
CDC dc, *pDC;
BITMAP bmp;
char color;
m_bitmap = new CBitmap();
m_bitmap->LoadBitmapW(IDB_BITMAP1);
m_bitmap->GetBitmap(&bmp);
pDC = this->GetDC();
dc.CreateCompatibleDC(pDC);
dc.SelectObject(m_bitmap);
pDC->BitBlt(100, 100, bmp.bmWidth, bmp.bmHeight, &dc,0 , 0, SRCCOPY);
m_bitmap->DeleteObject();
m_bitmap->Detach();
}

waitng for ur reply
Sweety Khan 8-Jun-10 4:28am    
this code just prints the original image
 
Share this answer
 
v2
Comments
Sweety Khan 9-Jun-10 4:25am    
thank u so much.

i made a button named Brightness on my main dialogue box. when any one click on it, it should display another dialogue box named Brightness.

For this, i right click on my project in the solution explorer->add->resource
From the Add Resource dialog box, double-click on Dialog
Change its ID to IDD_DLG_BRIGHTNESS and set its Caption to BRIGHTNESS.

Now what should i do next????

Waiting for ur reply.............
Sweety Khan 9-Jun-10 4:42am    
one thing i have also done that i right click on BRIGHTNESS dialogue->add class and specify class name as CBrightnessDlg
Sauro Viti 9-Jun-10 6:59am    
Add a message handler for the BM_CLICK notification from your button (you can do it by double-clicking the button on the dialog-editor), then inside the message handler add code to show your dialog, like this:

CBrightnessDlg dlg(this);
if (dlg.DoModal() == IDOK)
{
// Do something
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900