各位大虾:
我现在正在编一个处理灰度bmp图象的平台,只需要能读,显示,并把数据,以及图象的信息存入一个数组指针即可。急需大家能够提供思路和参考书!谢谢!
我现在正在编一个处理灰度bmp图象的平台,只需要能读,显示,并把数据,以及图象的信息存入一个数组指针即可。急需大家能够提供思路和参考书!谢谢!
解决方案 »
- 关于内存映谢文件的问题
- 菜单的中英文转换
- 紧急求救:如何将文本文件输入CHtmlView并且为文本中的邮件地址自动添加链接?
- 帮我改一个小小的程序
- 关于删除ActiveX控件的问题
- 如何使用waveOut系列函数,同时播放多个wave文件?
- 如何静态调方式调用dll,并使用dll中的函数,函数如何声明?
- 如何从一个文档模板的指针去访问该模板的CChildFrame,CDocument,CView的对象?
- 100分求Winsock完成端口服务程序的例子代码
- 现在的免驱动的摄像头怎样转成rgb24
- 如何在添加如何添加.h和.cpp文件?
- 我遇到头疼的问题,整了一天都不行!希望大家能帮帮忙!出出主意!!万分感谢!!
Environment: Developed with VC 5.
This is a sample simple BMP viewer. It only shows the minimum code for loading a BMP from a file and showing it in a window. It does not do everything; I get frustrated trying to figure out how to do something simple using a sample that does everything. In particular, it does not do palettes, which is something that seems to be important, but also seems to be relatively complicated. I am not a graphics expert so I invite an expert to create another article documenting correspondingly simple palette processing and/or printing and print preview processing.
Consistent with the theme of keeping things simple, I will simply describe how to create a sample program and supply relevant code here. You can use this code as a sample for use in your project or use this code and instructions to create a complete working sample.
To create a complete working sample, start by generating an MDI application with a CScrollView view. You might want to de-select the option for printing and print preview. Then:
Use ClassWizard to add processing for ID_FILE_NEW in your application class, but do nothing more with it, since we do not support bitmap creation.
Use ClassWizard to add processing for DeleteContents in your document class and add the following to it:
if (m_Bitmap.m_hObject != NULL)m_Bitmap.DeleteObject();Use ClassWizard to add processing for OnOpenDocument in your document class and replace all the generated code with the following:
if (IsModified())TRACE0("Warning: OnOpenDocument replaces an unsaved document\n");DeleteContents();BeginWaitCursor();HBITMAP hImage = (HBITMAP)LoadImage(NULL, lpszPathName, IMAGE_BITMAP,0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);EndWaitCursor();if (!hImage) {DWORD LastError = GetLastError();// Error message should be fomatted with LastError includedAfxMessageBox("LoadImage failed");return FALSE;}if (!m_Bitmap.Attach(hImage)) {AfxMessageBox("Bitmap could not be attached");return FALSE;}SetModifiedFlag(FALSE);UpdateAllViews(NULL);return TRUE;Add the following to the header for the document:
public:HBITMAP GetHandle() const {return (HBITMAP)m_Bitmap.m_hObject;};void SelectOldBitmap(CDC *pDCMem) {pDCMem->SelectObject(m_pOldBitmap);};void SelectBitmap(CDC *pDCMem){m_pOldBitmap=pDCMem->SelectObject(&m_Bitmap);};int GetBitmap(BITMAP* pBitMap) {return m_Bitmap.GetBitmap(pBitMap);};
protected:CBitmap m_Bitmap;CBitmap* m_pOldBitmap;Use ClassWizard to remove processing for OnInitialUpdate in your view class and delete the function from the code
Add to the view constructor:
SetScrollSizes(MM_TEXT, CSize(0, 0));Use ClassWizard to add processing for OnUpdate in your view class and add the following to it:
CBMPLookDoc* pDoc = GetDocument();CFrameWnd* pParentFrame = GetParentFrame();BITMAP BitMap;if (!pDoc->GetHandle())return;pDoc->GetBitmap(&BitMap);SetScrollSizes(MM_TEXT, CSize(BitMap.bmWidth, BitMap.bmHeight));pParentFrame->RecalcLayout();ResizeParentToFit();Finally, replace the OnDraw processing with the following:
CBMPLookDoc* pDoc = GetDocument();BITMAP BitMap;CDC DCMem;// Do not call CWnd::OnPaint() for painting messagesASSERT_VALID(pDoc);if (!pDoc->GetHandle())return;if (!DCMem.CreateCompatibleDC(pDC))TRACE0("DCMem.CreateCompatibleDC failed\n");pDoc->SelectBitmap(&DCMem);pDoc->GetBitmap(&BitMap);if (!pDC->BitBlt(0, 0, BitMap.bmWidth, BitMap.bmHeight, &DCMem, 0, 0, SRCCOPY))TRACE0("BitBlt failed\n");pDoc->SelectOldBitmap(&DCMem);DCMem.DeleteDC();You should also modify the "" and "" in the Document Template String to use appropriate file extensions and ".bmp" for the file extension. See CDocTemplate::GetDocString and Microsoft Knowledge Base article "INFO: Format of the Document Template String" (Article ID: Q129095) for information on Document Template Strings. The string resource id for the string is usually 129.
我以前做过一个简单的bmp图象读取、显示和简单处理的类
下面贴出的是其中的读取和显示部分,希望对你有帮助
//////////////读入图象///////////////////
BOOL CImage::LoadImage(LPCTSTR OpenFileName)
{ CFile file;
if(!file.Open(OpenFileName,CFile::modeRead|CFile::shareDenyWrite))
{
MessageBox(NULL,"BMP file open error!","warning",MB_OK);
return FALSE;
}
file.Read(&bf,sizeof(BITMAPFILEHEADER));
if(bf.bfType!=((WORD) ('M'<<8)|'B'))
{
MessageBox(NULL,"The file is not a BMP file!","warning",MB_OK);
return FALSE;
}
if(bf.bfSize!=file.GetLength())
{
MessageBox(NULL,"The BMP file header error!","warning",MB_OK);
return FALSE;
}
UINT uBmpInfoLen=(UINT) bf.bfOffBits-sizeof(BITMAPFILEHEADER);
if(m_bOpen) delete m_lpBitmap;
m_lpBitmap=(LPBITMAPINFO) new BYTE[uBmpInfoLen];
file.Read((LPVOID) m_lpBitmap,uBmpInfoLen);
if((* (LPDWORD)(m_lpBitmap))!=sizeof(BITMAPINFOHEADER))
{
MessageBox(NULL,"The BMP is not Windows 3.0 format!",
"warning",MB_OK);
return FALSE;
}
m_ImageHeight=m_lpBitmap->bmiHeader.biHeight;
m_ImageWidth =m_lpBitmap->bmiHeader.biWidth;
while(m_ImageWidth%4!=0) //让宽度为4的整数倍
m_ImageWidth++;
if(m_bOpen) delete m_imagedata;
m_imagedata= new BYTE[m_ImageHeight*m_ImageWidth];
file.ReadHuge(m_imagedata,m_ImageHeight*m_ImageWidth);
file.Close();
m_bOpen=TRUE;
m_bHist=FALSE;
return TRUE;}void CImage::DisplayImage(CDC *dc, int xSrc, int ySrc)
{
if(m_imagedata==NULL) return ;
HDC hdc=dc->m_hDC;
SetDIBitsToDevice(hdc,xSrc,ySrc,m_ImageWidth,m_ImageHeight,
0,0,0,m_ImageHeight,m_imagedata,m_lpBitmap,DIB_RGB_COLORS);
return;
void CDib::LoadFile(const char *pszDibFileName)
{
ASSERT(pszDibFileName);
if(m_pBitmapInfo)::GlobalFree(m_hDIB);//如果存在,释放掉
Init();//重新初始化
m_pszFileName = (char*)pszDibFileName; BITMAPFILEHEADER bitmapFileHeader;
CFile dibFile(pszDibFileName,CFile::modeRead);
try
{
UINT nBFHsize = dibFile.Read(
(void*)&bitmapFileHeader,sizeof (BITMAPFILEHEADER));
if(nBFHsize != sizeof(BITMAPFILEHEADER))
{
m_bValid = FALSE;
return;
}
}
catch(CFileException* e)
{
e->Delete();
} //try catch结构????? if(bitmapFileHeader.bfType == 0x4d42) //判断是否是bmp
{
AfxGetApp()->BeginWaitCursor();
DWORD dwFileLength = dibFile.GetLength();//计算文件长度
DWORD dwSize = dwFileLength - sizeof(BITMAPFILEHEADER);//计算剩余部分
m_hDIB = (HGLOBAL)::GlobalAlloc(GMEM_MOVEABLE,dwSize);
if(m_hDIB == 0)
m_bValid = FALSE;
BYTE *pbyDib = (BYTE*)::GlobalLock(m_hDIB);
if(pbyDib == NULL)
m_bValid = FALSE; try
{
DWORD dwReadSize = dibFile.Read((void*)pbyDib,dwSize);
dibFile.Close();
if(dwReadSize != dwSize)
{
m_bValid = FALSE;
::GlobalUnlock(m_hDIB);
::GlobalFree(m_hDIB);
Init();
return;
}
}
catch(CFileException* e)
{
e->Delete();
}
//以上为dib信息和图象数据分配全局内存m_hDIB,锁定内存
//以下要读取数据,并分析数据
m_pBitmapInfo = (BITMAPINFO*)pbyDib;
m_pBitmapInfoHeader = (BITMAPINFOHEADER*)pbyDib;//两个是一个地址
m_nWidth = m_pBitmapInfoHeader->biWidth;
m_nHeight = m_pBitmapInfoHeader->biHeight;
m_nBitCount = (BYTE)m_pBitmapInfoHeader->biBitCount;
m_nTotalColors = GetColorNumber(); //计算颜色区的指针
if(m_pBitmapInfoHeader->biClrUsed == 0)
m_pBitmapInfoHeader->biClrUsed = m_nTotalColors; m_pRGBQuad = (RGBQUAD*)(pbyDib + m_pBitmapInfoHeader->biSize);
DWORD dwColorTableSize = m_nTotalColors * sizeof(RGBQUAD); m_pDibData = pbyDib + m_pBitmapInfoHeader->biSize + dwColorTableSize;
//数据区指针 if(m_pRGBQuad == (RGBQUAD*)m_pDibData)
m_pRGBQuad = NULL;//对24位位图而言 m_pBitmapInfoHeader->biSizeImage = GetSize();
::GlobalUnlock(m_hDIB);
AfxGetApp()->EndWaitCursor();
m_bValid = TRUE;
}
else
{
AfxMessageBox("This is not a bitmap file!");
m_bValid = FALSE;
}
}int CDib::Draw(HDC hdc,int Xdest,int Ydest,int nDestWidth,int nDestHeight,
int XSrc,int Ysrc,int nSrcWidth,int nSrcHeight,UINT iUsage,
DWORD dwRop)
{
if(m_pRGBQuad)
{
HPALETTE hPalette = CreateBitmapPalette();
HPALETTE hOldPalette = ::SelectPalette(hdc,hPalette,FALSE);
::RealizePalette(hdc); int nScanLines = StretchDIBits(hdc,Xdest,Ydest,nDestWidth,
nDestHeight,XSrc,Ysrc,nSrcWidth,nSrcHeight,m_pDibData,
m_pBitmapInfo,iUsage,dwRop); ::SelectPalette(hdc,hOldPalette,FALSE);
::DeleteObject(hPalette);
return nScanLines;
}
else
return StretchDIBits(hdc,Xdest,Ydest,nDestWidth,
nDestHeight,XSrc,Ysrc,nSrcWidth,nSrcHeight,m_pDibData,
m_pBitmapInfo,iUsage,dwRop);
}
我显示过好好的