首页上一页 1 下一页尾页 3 条记录 1/1页
请教:《Visual C++开发实战1200例》实例536图像转换为字符
发表在VC++答疑区
2015-05-19
是否精华
是
否
版块置顶:
是
否
《Visual C++开发实战1200例》实例536图像转换为字符
源代码如下:
void CPictureToTextDlg::OnConvert()
{
CPaintDC dc(this);
long* m_pbmpdata;
long* p_tmp;
BITMAPINFO *m_pbmpinfo,m_bmp;
HBITMAP hbmp=(HBITMAP)LoadImage(::AfxGetResourceHandle(),"bitmap.bmp",
IMAGE_BITMAP,0, 0,LR_DEFAULTCOLOR|LR_LOADFROMFILE);
m_bmp.bmiHeader.biSize=sizeof(m_bmp.bmiHeader);
m_bmp.bmiHeader.biBitCount=0;
int bmpWidth;
int bmpHeight;
GetDIBits(dc.GetSafeHdc(),hbmp,0,1,NULL,&m_bmp,DIB_RGB_COLORS);
bmpWidth=m_bmp.bmiHeader.biWidth;
bmpHeight=m_bmp.bmiHeader.biHeight;
p_tmp=(long*)malloc(bmpWidth*bmpHeight*m_bmp.bmiHeader.biBitCount+sizeof(BITMAPINFO));
m_pbmpinfo=(BITMAPINFO*)p_tmp;
m_pbmpdata=p_tmp+sizeof(BITMAPINFO);
memcpy(p_tmp,(const void*)&m_bmp,sizeof(BITMAPINFOHEADER));
GetDIBits(dc.GetSafeHdc(),hbmp,0,m_bmp.bmiHeader.biHeight,m_pbmpdata,
m_pbmpinfo,DIB_RGB_COLORS);
long lngBackColor=RGB(255,255,255);
long lTemp;
int r,g,b;
CString strChar,strTemp;
for(int i=bmpHeight;i>1;i--)
{
for(int j=1;j<bmpWidth;j++)
{
lTemp=m_pbmpdata[(j-1)+(i-1)*bmpWidth];
r=GetRValue(lTemp);
g=GetGValue(lTemp);
b=GetBValue(lTemp);
if(r>224 || g >224 || r >224)
strChar=" ";
else if(r>192 || g >192 || r >192 )
strChar="#";
else if(r>160 || g >160 || r >160 )
strChar="%";
else if(r>128 || g >128 || r >128 )
strChar="$";
else if(r>96 || g >96 || r >96 )
strChar="8";
else if(r>64 || g >64 || r >64 )
strChar="X";
else if(r>32 || g >32 || r >32 )
strChar="?";
else
strChar="?";
m_edit.GetWindowText(strTemp);
strTemp=strTemp+strChar;
m_edit.SetWindowText(strTemp);
}
m_edit.GetWindowText(strTemp);
strTemp=strTemp+"\r\n";
m_edit.SetWindowText(strTemp);
}
}
我想把前面代码换成获取图像上下文那样的,怎么做?
就跟下面灰度化实例类似的
void CTang511Dlg::OnOK()
{
CDC* pDC = m_image.GetDC();
CRect m_rect ;
m_image.GetClientRect(m_rect);
COLORREF m_color;
DWORD m_gray;
BYTE r,g,b;
for (int i = 0; i<m_rect.right;i++)
for (int j = 0;j<m_rect.bottom;j++)
{
m_color = pDC->GetPixel(i,j);
r = GetRValue(m_color);
g = GetGValue(m_color);
b = GetRValue(m_color);
m_gray = (0.38*r+0.49*g+0.1*b);
m_color = RGB(m_gray,m_gray,m_gray);
pDC->SetPixel(i,j,m_color);
}
}
源代码如下:
void CPictureToTextDlg::OnConvert()
{
CPaintDC dc(this);
long* m_pbmpdata;
long* p_tmp;
BITMAPINFO *m_pbmpinfo,m_bmp;
HBITMAP hbmp=(HBITMAP)LoadImage(::AfxGetResourceHandle(),"bitmap.bmp",
IMAGE_BITMAP,0, 0,LR_DEFAULTCOLOR|LR_LOADFROMFILE);
m_bmp.bmiHeader.biSize=sizeof(m_bmp.bmiHeader);
m_bmp.bmiHeader.biBitCount=0;
int bmpWidth;
int bmpHeight;
GetDIBits(dc.GetSafeHdc(),hbmp,0,1,NULL,&m_bmp,DIB_RGB_COLORS);
bmpWidth=m_bmp.bmiHeader.biWidth;
bmpHeight=m_bmp.bmiHeader.biHeight;
p_tmp=(long*)malloc(bmpWidth*bmpHeight*m_bmp.bmiHeader.biBitCount+sizeof(BITMAPINFO));
m_pbmpinfo=(BITMAPINFO*)p_tmp;
m_pbmpdata=p_tmp+sizeof(BITMAPINFO);
memcpy(p_tmp,(const void*)&m_bmp,sizeof(BITMAPINFOHEADER));
GetDIBits(dc.GetSafeHdc(),hbmp,0,m_bmp.bmiHeader.biHeight,m_pbmpdata,
m_pbmpinfo,DIB_RGB_COLORS);
long lngBackColor=RGB(255,255,255);
long lTemp;
int r,g,b;
CString strChar,strTemp;
for(int i=bmpHeight;i>1;i--)
{
for(int j=1;j<bmpWidth;j++)
{
lTemp=m_pbmpdata[(j-1)+(i-1)*bmpWidth];
r=GetRValue(lTemp);
g=GetGValue(lTemp);
b=GetBValue(lTemp);
if(r>224 || g >224 || r >224)
strChar=" ";
else if(r>192 || g >192 || r >192 )
strChar="#";
else if(r>160 || g >160 || r >160 )
strChar="%";
else if(r>128 || g >128 || r >128 )
strChar="$";
else if(r>96 || g >96 || r >96 )
strChar="8";
else if(r>64 || g >64 || r >64 )
strChar="X";
else if(r>32 || g >32 || r >32 )
strChar="?";
else
strChar="?";
m_edit.GetWindowText(strTemp);
strTemp=strTemp+strChar;
m_edit.SetWindowText(strTemp);
}
m_edit.GetWindowText(strTemp);
strTemp=strTemp+"\r\n";
m_edit.SetWindowText(strTemp);
}
}
我想把前面代码换成获取图像上下文那样的,怎么做?
就跟下面灰度化实例类似的
void CTang511Dlg::OnOK()
{
CDC* pDC = m_image.GetDC();
CRect m_rect ;
m_image.GetClientRect(m_rect);
COLORREF m_color;
DWORD m_gray;
BYTE r,g,b;
for (int i = 0; i<m_rect.right;i++)
for (int j = 0;j<m_rect.bottom;j++)
{
m_color = pDC->GetPixel(i,j);
r = GetRValue(m_color);
g = GetGValue(m_color);
b = GetRValue(m_color);
m_gray = (0.38*r+0.49*g+0.1*b);
m_color = RGB(m_gray,m_gray,m_gray);
pDC->SetPixel(i,j,m_color);
}
}