www.pudn.com > 图像处理源代码.rar > ImgView.cpp


// ImgView.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "tuxiang.h" 
#include "ImgView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CImgView dialog 
 
 
CImgView::CImgView(MYIMAGE *pImage,CWnd* pParent /*=NULL*/) 
	: CDialog(CImgView::IDD, pParent) 
{ 
	img=pImage; 
	Create(CImgView::IDD,pParent); 
	//{{AFX_DATA_INIT(CImgView) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
} 
 
 
void CImgView::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CImgView) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CImgView, CDialog) 
	//{{AFX_MSG_MAP(CImgView) 
	ON_WM_SIZE() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CImgView message handlers 
 
BOOL CImgView::DisplayImage() 
{ 
	if(img==NULL) 
	{ 
		MessageBox("图象数据为空"); 
		return FALSE; 
	} 
	 
	RECT client; 
	SIZE clientsize; 
	POINT center; 
 
	GetClientRect(&client); 
	clientsize.cx = client.right - client.left + 1; 
	clientsize.cy = client.bottom - client.top + 1; 
	center.x = clientsize.cx /2; 
	center.y = clientsize.cy /2; 
 
	BYTE *current=img->ImgData; 
	COLORREF color; 
 
	//显示象素vert,hori,color 
	CClientDC* dc=new CClientDC(this); 
 
	dc->FillSolidRect (&client,RGB(255,255,255)); 
 
	for(int vert=img->Height;vert>0 ;vert--)	 
		for(int hori=0 ;horiWidth ; hori++) 
		{	//24位色,分别读取r,g,b生成颜色值 
			if(img->ColorTabNumber == 0 && img->BitCount ==24)	 
			{ 
				int r,g,b; 
				b=*current++; 
				g=*current++; 
				r=*current++; 
				color=b*0x010000+g*0x000100+r; 
			} 
			else	//其他读取索引值,查颜色表生成颜色值 
			{ 
				unsigned int index=*current++; 
				color=img->ColorTab[index].rgbBlue*0x10000+ 
						img->ColorTab[index].rgbGreen*0x100 + 
						img->ColorTab[index].rgbRed; 
			} 
 
			dc->SetPixelV (hori+center.x-img->Width / 2, 
						   vert+center.y - img->Height /2, 
						   color); 
		} 
	return TRUE; 
} 
 
void CImgView::OnSize(UINT nType, int cx, int cy)  
{ 
	CDialog::OnSize(nType, cx, cy); 
	 
	// TODO: Add your message handler code here 
	DisplayImage();	 
}