#include "stdafx.h"
#include "CDib.h"
#include "windowsx.h"
#include "math.h"册返
#define WIDTHBYTES(bits) (((bit)+31)/32*4)
CDib::CDib()
{
size=0
}
CDib::~CDib()
{
GlobalFreePtr(m_pBitmapInfo)
}
void CDib::LoadFile(const char *dibFileName)
{
strcpy(m_fileName,dibFileName)
CFile dibFile(m_fileName,CFile::modeRead)
dibFile.Read((void*)&bitmapFileHeader,sizeof(BITMAPFILEHEADER))
if (bitmapFileHeader.bfType == 0x4d42)
{
DWORD fileLength=dibFile.GetLength()
size=fileLength-sizeof(BITMAPFILEHEADER)
pDib=(BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, size)
dibFile.Read((void*)pDib,size)
dibFile.Close()
m_pBitmapInfo=(BITMAPINFO*)pDib
m_pBitmapInfoHeader=(BITMAPINFOHEADER*)pDib
m_pRGB=(RGBQUAD*)(pDib+m_pBitmapInfoHeader->biSize)
int m_numberOfColors=GetNumberOfColors()
if(m_pBitmapInfoHeader->biClrUsed==0)
m_pBitmapInfoHeader->biClrUsed=m_numberOfColors
DWORD colorTableSize=m_numberOfColors*sizeof(RGBQUAD)
m_pData=pDib+m_pBitmapInfoHeader->轮链biSize+colorTableSize
if(m_pRGB==(RGBQUAD*)m_pData)
m_pRGB=NULL
m_pBitmapInfoHeader->biSizeImage=GetSize()
m_valid=TRUE
}
else
{
m_valid=FALSE
AfxMessageBox("This isn't a bitmap file!")
}
}
BOOL CDib::IsValid()
{
return m_valid
}
char *CDib::GetFileName()
{
return m_fileName
}
UINT CDib::GetWidth()
{
return (UINT)m_pBitmapInfoHeader->biWidth
}
UINT CDib::GetHeight()
{
return (UINT)m_pBitmapInfoHeader->biHeight
}
DWORD CDib::GetSize()
{
if(m_pBitmapInfoHeader->biSizeImage!=0)
return m_pBitmapInfoHeader->biSizeImage
else
{
DWORD height=(DWORD)GetHeight()
DWORD width=(DWORD)GetWidth()
return height*width
}
}
UINT CDib::GetNumberOfColors()
{
int numberOfColors
if((m_pBitmapInfoHeader->biClrUsed==0)&&(m_pBitmapInfoHeader->biBitCount<9))
{
switch(m_pBitmapInfoHeader->biBitCount)
{
case 1:numberOfColors=2
break
case 4:numberOfColors=16
break
case 8:numberOfColors=256
}
}
else
numberOfColors=(int)m_pBitmapInfoHeader->biClrUsed
return numberOfColors
}
BYTE *CDib::GetData()
{
return m_pData
}
RGBQUAD *CDib::GetRGB()
{
return m_pRGB
}
BITMAPINFO *CDib::GetInfo()
{
return m_pBitmapInfo
}
WORD CDib::PaletteSize(LPBYTE lpDIB)
{
return (DIBNumColors(lpDIB)*sizeof(RGBTRIPLE))
}
WORD CDib::DIBNumColors(LPBYTE lpDIB)
{
WORD wBitCount
wBitCount=((LPBITMAPCOREHEADER)lpDIB)->bcBitCount
switch(wBitCount)
{
case 1:return 2
case 4:return 16
case 8:return 256
default:return 0
}
}
void CDib::SaveFile(const CString filename)
{
strcpy(m_fileName,filename)
CFile dibFile(m_fileName,CFile::modeCreate|CFile::modeWrite)
dibFile.Write((void*)&bitmapFileHeader,sizeof(BITMAPFILEHEADER))
dibFile.Write((void*)pDib,size)
dibFile.Close()
}
//CDib.h
#ifndef __CDIB_H
#define __CDIB_H
class CDib : public CObject
{
public:
RGBQUAD*m_pRGB
BYTE *m_pData
UINTm_numberOfColors
BOOLm_valid
BITMAPFILEHEADER bitmapFileHeader
BITMAPINFOHEADER *m_pBitmapInfoHeader
BITMAPINFO *m_pBitmapInfo
BYTE *pDib
DWORD size
public:
CDib()
~CDib()
char m_fileName[256]
char *GetFileName()
BOOL IsValid()
DWORD GetSize()
UINT GetWidth()
UINT GetHeight()
UINT GetNumberOfColors()
RGBQUAD *GetRGB()
BYTE *GetData()
BITMAPINFO *GetInfo()
WORD PaletteSize(LPBYTE lpDIB)
WORD DIBNumColors(LPBYTE lpDIB)
void SaveFile(const CString filename)
public:
void LoadFile(const char *dibFileName)
}
#endif
//View.cpp
// TestView.cpp : implementation of the CTestView class
//
#include "stdafx.h"
#include "Test.h"
#include "TestDoc.h"
#include "TestView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestView
IMPLEMENT_DYNCREATE(CTestView, CView)
BEGIN_MESSAGE_MAP(CTestView, CView)
//{{AFX_MSG_MAP(CTestView)
ON_COMMAND(ID_YUANTU, OnYuantu)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction
CTestView::CTestView()
{
// TODO: add construction code here
}
CTestView::~CTestView()
{
}
BOOL CTestView::PreCreateWindow(CREATESTRUCT&cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs)
}
/////////////////////////////////////////////////////////////////////////////
// CTestView drawing
void CTestView::OnDraw(CDC* pDC)
{
//CTestDoc* pDoc = GetDocument()
//ASSERT_VALID(pDoc)
// TODO: add draw code for native data here
if(state1==1)
{
CBitmap m_bitmap
HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,_T(filename),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE)
m_bitmap.Attach(hBitmap)
CDC dcImage
if(!dcImage.CreateCompatibleDC(pDC))
return
BITMAP bm
m_bitmap.GetBitmap(&bm)
dcImage.SelectObject(&m_bitmap)
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcImage,0,0,SRCCOPY)
}
}
/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics
#ifdef _DEBUG
void CTestView::AssertValid() const
{
CView::AssertValid()
}
void CTestView::Dump(CDumpContext&dc) const
{
CView::Dump(dc)
}
CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)))
return (CTestDoc*)m_pDocument
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers
void CTestView::OnYuantu()
{
// TODO: Add your command handler code here
CTestDoc *pDoc=GetDocument()
ASSERT_VALID(pDoc)
filename=pDoc->filename
state1=1
Invalidate()
}
用VC读取bmp位图文件的源代码 :CPaintDC dc(this)
CDC memDC
CBitmap bmp
HBITMAP hbmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),strPath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE)
bmp.Attach(hbmp)
memDC.CreateCompatibleDC(&dc)
memDC.SelectObject(&bmp)
dc.BitBlt(0,0,800,600,&memDC,0,0,SRCCOPY)
memDC.DeleteDC()
其中灶樱strPath为位图的地缓镇址如:strPath="隐哪丛E:\\test.bmp"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)