请帮助我将位图转换为灰度 [英] Please help me converting bitmap to greyscale
本文介绍了请帮助我将位图转换为灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用MFC将位图图像转换为灰度图像,但得到了一些错误帮助.这是我的代码:
I''m converting a bitmap image into grey scale using MFC but got some errors kindly help. Here is my code:
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])''
推荐答案
尝试以下操作:
Try this:
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);
}
您正在向AfxMessageBox
函数传递 ANSI 字符串,但是该函数需要 ANSI 或 UNICODE 字符串,具体取决于您的项目设置.
只需更改行:
You are passing an ANSI string to heAfxMessageBox
function, but that function require an ANSI or UNICODE string depending on your project settings.
Simply change the line:
AfxMessageBox("Fail to convert grayscale");
至:
to:
AfxMessageBox(_T("Fail to convert grayscale"));
请注意,宏_T(x)
获取一个字符串,如果您的项目使用 MBCS字符集,则不修改该字符串,否则,如果您的项目使用 UNICODE字符集,则宏会添加一个L
在表示宽字符字符串
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
error C3861: ''ceil'': identifier not found
->的字符串(例如L"my string"
)之前.将#include <math.h>
添加到stdafx.h
或.cpp
文件的顶部
error C2065: ''IDC_STATIC_Processed'': undeclared identifier
->确保标识符正确,在.cpp
文件顶部添加#include "resource.h"
error C3861: ''ceil'': identifier not found
--> add#include <math.h>
into yourstdafx.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
这篇关于请帮助我将位图转换为灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文