Vc++打开bmp格式的图像(step-by-step)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

一:Vc++打开bmp格式的图像1.打开MicrosoftVisualc++6.02.按“文件”的“新建”按钮,在菜单栏上的“工程”按钮上选择MFCAppWizad(exe),自己新建一个工程名称1.在菜单栏上的“查看”上点“建立类向导”,用类向导ClassWizard在Pp.cpp中添加消息事件ID_FILE_OPEN在//TODO:Addyourcommandhandlercodehere后加上CFileDialogFileDlg(TRUE,*.bmp,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T(Bitmap(*.BMP)|*.BMP|));if(FileDlg.DoModal()==IDOK){CStringcstrFileName=FileDlg.GetPathName();OpenDocumentFile(cstrFileName);}2.在菜单栏上的“查看”上点“建立类向导”,用类向导ClassWizard在Pp.cpp中添加消息响应事件OpenDocumentFile用类向导ClassWizard在PpDoc.cpp中添加消息响应事件OnOpenDocument,并添加图像处理函数ReadImage在//TODO:Addyourspecializedcreationcodehere后加上ReadImage(lpszPathName);再在后面加上:BOOLCPpDoc::ReadImage(constchar*szFileName){CFilefile;CFileExceptionfe;intnReadFromFile;longlImageSize;if(!file.Open(szFileName,CFile::modeRead|CFile::shareDenyWrite,&fe)){ReportSaveLoadException(szFileName,&fe,FALSE,AFX_IDP_FAILED_TO_OPEN_DOC);returnFALSE;…………}调试之后出现了没有定义的错误,所以要在他的头文件PpDoc.h中对函数和变量的申明用类向导ClassWizard在PpView.cpp中添加绘图消息响应函数OnDraw,用于处理8位和24位数字图像a.在PpView.cpp中添加8位(DisplayBmp8)和24位(DisplayBmp24)数字图像处理函数voidCPpView::OnDraw(CDC*pDC){CPpDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahereif(pDoc-bImageLoaded){if(pDoc-BitCount==8)//8bit{DisplayBmp8(pDoc,pDC);}else//24bit{DisplayBmp24(pDoc,pDC);}}}voidCPpView::DisplayBmp8(CPpDoc*pDoc,CDC*pDC){CRectrect;CSizesizeTotal;intnColorIndex,i,j,Width;BYTEbtBlue,btGreen,btRed;COLORREFcolor;rect.top=0;rect.left=0;rect.right=pDoc-BmpWidth-1;rect.bottom=pDoc-BmpHeight-1;if(pDoc-BmpWidth%4!=0){Width=((pDoc-BmpWidth/4)+1)*4;}else{Width=pDoc-BmpWidth;}for(j=0;jpDoc-BmpHeight;j++)for(i=0;ipDoc-BmpWidth;i++){nColorIndex=pDoc-pImage[j*Width+i];btBlue=pDoc-pBitMapInfo-bmiColors[nColorIndex].rgbBlue;btGreen=pDoc-pBitMapInfo-bmiColors[nColorIndex].rgbGreen;btRed=pDoc-pBitMapInfo-bmiColors[nColorIndex].rgbRed;color=RGB(btBlue,btGreen,btRed);pDC-SetPixel(i,rect.bottom-j,color);}}voidCPpView::DisplayBmp24(CPpDoc*pDoc,CDC*pDC){CRectrect;CSizesizeTotal;inti,j,Width;BYTEbtBlue,btGreen,btRed;COLORREFcolor;rect.top=0;rect.left=0;rect.right=pDoc-BmpWidth-1;rect.bottom=pDoc-BmpHeight-1;if(pDoc-BmpWidth%4!=0){Width=((pDoc-BmpWidth*3/4)+1)*4;}else{Width=pDoc-BmpWidth*3;}for(j=0;jpDoc-BmpHeight;j++)for(i=0;ipDoc-BmpWidth;i++){btBlue=pDoc-pImage[j*Width+i*3];btGreen=pDoc-pImage[j*Width+i*3+1];btRed=pDoc-pImage[j*Width+i*3+2];color=RGB(btRed,btGreen,btBlue);pDC-SetPixel(i,rect.bottom-j,color);}}b调试后出现未定义的错误,DisplayBmp8和DisplayBmp24需在头文件ProjectView.cpp中进行声明在PpDoc.cpp的构造函数中申请绘图窗口所需的内存空间在PpDoc.cpp的析造函数中释放申请绘图窗口所需的内存空间if(bImageLoaded==TRUE){GlobalFree(pImage);}LocalFree(pBitMapInfo);窗口的最大化显示,只要在Pp.cpp中修改ShowWindow的参数pMainFrameShowWindow(SW_MAXIMIZE);不打开一个空的窗口,只要调用CCommandLineInfo类的成员函数m_nShellCommandcmdInfo.m_nShellCommand=CCommandLineInfo::FileNothing;可以在PpView.cpp的OnInitialUpdate函数中调整窗口大小为图像本身大小CPpDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);sizeTotal.cy=pDoc-BmpHeight;sizeTotal.cx=pDoc-BmpWidth;SetScrollSizes(MM_TEXT,sizeTotal);ResizeParentToFit(TRUE);在ChildFrm.cpp的PreCreateWindow函数中可初始化子窗口大小BOOLCChildFrame::PreCreateWindow(CREATESTRUCT&cs){//TODO:ModifytheWindowclassorstylesherebymodifying//theCREATESTRUCTcscs.cx=1000;cs.cy=1000;if(!CMDIChildWnd::PreCreateWindow(cs))returnFALSE;returnTRUE;}最终调试后:二:图像基本信息的获取LongToString函数的定义CStringCBaseInfoDlg::LongToString(longdata){CStringstr;intdec,sign;str=_ecvt(data,10,&dec,&sign);//AfxMessageBox(str.Left(dec));if(data!=0)returnstr.Left(dec);elsereturn0;}在新建类CBaseInfoDlg添加SetInfo函数voidCBaseInfoDlg::SetInfo(CMyTestDoc*pDoc){CStringcsTemp;if(pDoc-BitMapFileHeader.bfType==0x4D42){m_Style=BMP;}csTemp=LongToString(pDoc-BitMapFileHeader.bfSize);m_FileSize=csTemp+B;csTemp=LongToString(pDoc-BitMapFileHeader.bfOffBits);m_DataOff=csTemp+-十进制;csTemp=LongToString(pDoc-pBitMapInfo-bmiHeader.biWidth);m_Width=csTemp+像素;csTemp=LongToString(pDoc-pBitMapInfo-bmiHeader.biHeight);m_Height=csTemp+像素;csTemp=LongToString(pDoc-pBitMapInfo-bmiHeader.biBitCount);m_PixelDep=csTemp+bit;}LongToString和SetInfo需在BMPDLG.h中进行定义public:CBMPDLG(CWnd*pParent=NULL);//standardconstructorCStringLongToString(longdata);voidSetInfo(CPpDoc*pDoc);在BMPDLG.h中加PpDoc的头文件#include“PpDoc.h”因为有这个申明voidSetInfo(CPpDoc*pDoc);在PpView.cpp中进行对BMPDLG头文件的定义,添加#includeBMPDLG.h在PpView.cpp中的voidCPpView::OnBaseinfoInfo(){CPpDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);CBMPDLGdlg;//声明类对象dlgdlg.SetInfo(pDoc);//调用CBMPDLG类中的SetInfo函数dlg.DoModal();//显示图像信息对话框}三.直方图在新建的Dialog中建立类向导HistDlg在HistDlg.cpp中建立类向导在OnPaint()函数中写程序inti,j;if(BitCount==8){longnHist[256];//分配256字节的空间for(i=0;i256;i++){nHist[i]=0;}for(i=0;inBmpHeight;i++)for(j=0;jnBmpWidth;j++){nHist[pImage[i*nWidth+j]]++;}longhmax=0;doublerate;intth=0;CStringstr;for(i=0;i=255;i++){CStringCBMPHIST::LongToString(longdata){CStringstr;intdec,sign;str=_ecvt(data,10,&dec,&sign);returnstr.Left(dec);}在他的头文件中定义:CStringLongToString(longdata);BYTE*pImage;longnHist[256];longnBmpWid

1 / 57
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功