CBitmap *pBitmap=CBitmap::FromHandle(hBitmap)
我已經得到了一個CBitmap類對象的指針,但是我怎麼才能把它分解成
BITMAPFILEHEADER
BITMAPINFOHEADER
RGBQUAD
& DIBits 呢???? Thanks!
5 个解决方案
#1
还不如做个自己封装得类
下面给你一个封装得类
#ifndef _DIB_H
#define _DIB_H
const int nMaxGray=255;
inline int WIDTHBYTES(int i) { return (i%4)? (i-i%4+4) : i ; }
//ÓÃÓÚÏÔʾBMPµÄCDibÀà
class CDib: public CObject
{
public:
int GetLineSize();
int GetBitCount () { return m_pBmInfoHeader->biBitCount; }
CDib();
CDib(const char* fileName); //ÒÔÎļþÃûΪ²ÎÊýµÄ¹¹Ô캯Êý
~CDib();
BOOL CopyFromDib(CDib * pOldDib,BOOL sizeChanged=FALSE,
UINT newWidth=0, UINT newHeight=0); //¿½±´Î»Í¼
BOOL LoadFromFile(LPCTSTR fileName); //´ÓÎļþ¶ÁÈëλͼ
BOOL SaveToFile(LPCTSTR fileName); //°ÑλͼдÈëÎļþ
int GetAt(int x,int y); //»ñµÃÖ¸¶¨µãµÄÑÕÉ«Öµ
int SetAt(int x,int y,int value); //ÉèÖÃÖ¸¶¨µãµÄÑÕÉ«Öµ
//»ñµÃλͼµÄ¿í¶È
int Width() { return m_pBmInfoHeader->biWidth; }
//»ñµÃλͼµÄ¸ß¶È
int Height() { return m_pBmInfoHeader->biHeight; }
//»ñµÃλͼµÄʵ¼Ê´óС
DWORD ImageSize() { return m_pBmInfoHeader->biSizeImage; }
//»ñµÃλͼʹÓõÄÑÕÉ«Êý
DWORD UsedColors(){ return (DWORD) m_pBmInfoHeader->biClrUsed; }
DWORD BMPSize(){ return (DWORD)m_BmFileHeader.bfSize;}
DWORD ImageDataSize(); //¼ÆËãͼÏñÊý¾Ý×Ö½ÚÊý
//»ñµÃÐÅϢͷָÕë
LPBITMAPINFOHEADER GetInfoHeaderPtr() { return m_pBmInfoHeader; }
//»ñµÃÐÅÏ¢Ö¸Õë
LPBITMAPINFO GetInfoPtr() { return m_pBmInfo; }
//»ñµÃÑÕÉ«²éÕÒ±íÖ¸Õë
LPRGBQUAD GetRGBTablePtr() { return m_pRGBTable; }
//»ñµÃµãÊý×éµÄÖ¸Õë
BYTE * GetBmBitsPtr() { return m_pBmBits; }
//·µ»ØÊÇ·ñÐèÒªµ÷É«°å
BOOL NeedPalette() { return m_bNeedPalette; }
//·µ»ØλͼµÄµ÷É«°å
HPALETTE GetDibPalette() { return m_hPalette; }
//ÏÔʾÓÉCDib·â×°µÄλͼ
int ShowDib(CDC * pDC,CRect& rect,int bStretch);
BOOL EdgeEnhance(int Strength, int nAlgorithm);
//private:
public:
void Initialize(); //³õʼ»¯º¯Êý
void Empty(); //Çå¿Õº¯Êý
HPALETTE CreateDibPalette(); //´´½¨µ÷É«°å£¬·µ»ØÆä¾ä±ú
//private:
public:
BOOL SetBmBitsPtr(BYTE * pDib);
BITMAPFILEHEADER m_BmFileHeader;
LPBITMAPINFO m_pBmInfo; //λͼÐÅÏ¢Ö¸Õë
LPBITMAPINFOHEADER m_pBmInfoHeader; //λͼÐÅϢͷָÕë
RGBQUAD * m_pRGBTable; //λͼÑÕÉ«²éÕÒ±íÖ¸Õë
BYTE * m_pBmBits; //λͼµãÊý×é
BOOL m_bNeedPalette; //λͼÊÇ·ñÐèÒªµ÷É«°å
HPALETTE m_hPalette; //λͼµÄµ÷É«°å
};
#endif
下面给你一个封装得类
#ifndef _DIB_H
#define _DIB_H
const int nMaxGray=255;
inline int WIDTHBYTES(int i) { return (i%4)? (i-i%4+4) : i ; }
//ÓÃÓÚÏÔʾBMPµÄCDibÀà
class CDib: public CObject
{
public:
int GetLineSize();
int GetBitCount () { return m_pBmInfoHeader->biBitCount; }
CDib();
CDib(const char* fileName); //ÒÔÎļþÃûΪ²ÎÊýµÄ¹¹Ô캯Êý
~CDib();
BOOL CopyFromDib(CDib * pOldDib,BOOL sizeChanged=FALSE,
UINT newWidth=0, UINT newHeight=0); //¿½±´Î»Í¼
BOOL LoadFromFile(LPCTSTR fileName); //´ÓÎļþ¶ÁÈëλͼ
BOOL SaveToFile(LPCTSTR fileName); //°ÑλͼдÈëÎļþ
int GetAt(int x,int y); //»ñµÃÖ¸¶¨µãµÄÑÕÉ«Öµ
int SetAt(int x,int y,int value); //ÉèÖÃÖ¸¶¨µãµÄÑÕÉ«Öµ
//»ñµÃλͼµÄ¿í¶È
int Width() { return m_pBmInfoHeader->biWidth; }
//»ñµÃλͼµÄ¸ß¶È
int Height() { return m_pBmInfoHeader->biHeight; }
//»ñµÃλͼµÄʵ¼Ê´óС
DWORD ImageSize() { return m_pBmInfoHeader->biSizeImage; }
//»ñµÃλͼʹÓõÄÑÕÉ«Êý
DWORD UsedColors(){ return (DWORD) m_pBmInfoHeader->biClrUsed; }
DWORD BMPSize(){ return (DWORD)m_BmFileHeader.bfSize;}
DWORD ImageDataSize(); //¼ÆËãͼÏñÊý¾Ý×Ö½ÚÊý
//»ñµÃÐÅϢͷָÕë
LPBITMAPINFOHEADER GetInfoHeaderPtr() { return m_pBmInfoHeader; }
//»ñµÃÐÅÏ¢Ö¸Õë
LPBITMAPINFO GetInfoPtr() { return m_pBmInfo; }
//»ñµÃÑÕÉ«²éÕÒ±íÖ¸Õë
LPRGBQUAD GetRGBTablePtr() { return m_pRGBTable; }
//»ñµÃµãÊý×éµÄÖ¸Õë
BYTE * GetBmBitsPtr() { return m_pBmBits; }
//·µ»ØÊÇ·ñÐèÒªµ÷É«°å
BOOL NeedPalette() { return m_bNeedPalette; }
//·µ»ØλͼµÄµ÷É«°å
HPALETTE GetDibPalette() { return m_hPalette; }
//ÏÔʾÓÉCDib·â×°µÄλͼ
int ShowDib(CDC * pDC,CRect& rect,int bStretch);
BOOL EdgeEnhance(int Strength, int nAlgorithm);
//private:
public:
void Initialize(); //³õʼ»¯º¯Êý
void Empty(); //Çå¿Õº¯Êý
HPALETTE CreateDibPalette(); //´´½¨µ÷É«°å£¬·µ»ØÆä¾ä±ú
//private:
public:
BOOL SetBmBitsPtr(BYTE * pDib);
BITMAPFILEHEADER m_BmFileHeader;
LPBITMAPINFO m_pBmInfo; //λͼÐÅÏ¢Ö¸Õë
LPBITMAPINFOHEADER m_pBmInfoHeader; //λͼÐÅϢͷָÕë
RGBQUAD * m_pRGBTable; //λͼÑÕÉ«²éÕÒ±íÖ¸Õë
BYTE * m_pBmBits; //λͼµãÊý×é
BOOL m_bNeedPalette; //λͼÊÇ·ñÐèÒªµ÷É«°å
HPALETTE m_hPalette; //λͼµÄµ÷É«°å
};
#endif
#2
#include "stdafx.h"
#include "windows.h"
#include "windowsx.h"
#include "Dib.h"
CDib::CDib()
{
Initialize();
}
CDib::CDib(const char* fileName)
{
Initialize();
LoadFromFile(fileName);
}
void CDib::Initialize()
{
m_pBmInfo = 0;
m_pBmInfoHeader = 0;
m_pRGBTable = 0;
m_pBmBits = 0;
m_bNeedPalette = FALSE;
m_hPalette = NULL;
}
CDib::~CDib()
{
Empty();
}
//
void CDib::Empty()
{
if(m_pBmInfo)
GlobalFreePtr(m_pBmInfo); //
if(m_bNeedPalette && m_hPalette) // DeleteObject(m_hPalette);
}
//
BOOL CDib::LoadFromFile (LPCTSTR fileName)
{
CFile file;
//
if(file.Open(fileName, CFile::modeRead|CFile::shareDenyWrite)!=TRUE)
{
CString errorStr;
errorStr.Format(_T("´ò¿ª%s´íÎó"), (LPCTSTR)fileName);
// AfxMessageBox(errorStr);
//file.Close();
return FALSE;
}
//
BITMAPFILEHEADER bmFileHeader;
file.Read((void*)&bmFileHeader, sizeof(bmFileHeader));
m_BmFileHeader = bmFileHeader;
//
if (bmFileHeader.bfType != 0x4d42)
{
CString errorStr;
errorStr.Format("%s ²»ÊÇλͼÎļþ!!!", (LPCTSTR)fileName);
//AfxMessageBox(errorStr);
file.Close();
return FALSE;
}
DWORD fileLength = file.GetLength();
DWORD dibSize = fileLength - sizeof(bmFileHeader);
BYTE* pDib = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, dibSize);
file.Read((void*)pDib, dibSize);
file.Close();
m_pBmInfo = (LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
if (m_pBmInfoHeader->biSizeImage == 0)
m_pBmInfoHeader->biSizeImage
= m_pBmInfoHeader->biWidth * m_pBmInfoHeader->biHeight;
if(m_pBmInfoHeader->biClrUsed == 0)
m_pBmInfoHeader->biClrUsed = DWORD(1 << m_pBmInfoHeader->biBitCount);
if(m_pBmInfoHeader->biClrUsed <= 256)
{
m_bNeedPalette = TRUE;
m_pRGBTable = (RGBQUAD*)(pDib + m_pBmInfoHeader->biSize);
m_hPalette = CreateDibPalette();
}
DWORD clrTableSize = m_bNeedPalette ? UsedColors() * sizeof(RGBQUAD) : 0;
m_pBmBits = pDib + m_pBmInfoHeader->biSize + clrTableSize;
return TRUE;
}
BOOL CDib::SaveToFile(LPCTSTR fileName)
{
CFile file(fileName, CFile::modeWrite|CFile::modeCreate|CFile::shareDenyWrite);
BITMAPFILEHEADER bmFileHeader;
bmFileHeader.bfType = 0x4d42;
UINT height=Height();
UINT width=Width();
UINT line=WIDTHBYTES(width);
DWORD clrTableSize = m_bNeedPalette ? UsedColors() * sizeof(RGBQUAD) : 0;
DWORD dibInfoSize=m_pBmInfoHeader->biSize+clrTableSize;
DWORD dibSize=dibInfoSize+ImageDataSize();
bmFileHeader.bfSize = DWORD(dibSize+sizeof(BITMAPFILEHEADER));
bmFileHeader.bfReserved1 = bmFileHeader.bfReserved2 = 0;
bmFileHeader.bfOffBits = DWORD (dibInfoSize+sizeof(BITMAPFILEHEADER));
file.Write((void*)&bmFileHeader, sizeof(bmFileHeader));
file.WriteHuge((void*)m_pBmInfo, dibSize);
file.Close();
return TRUE;
}
BOOL CDib::CopyFromDib(CDib * pOldDib,BOOL bSizeChanged,
UINT newWidth, UINT newHeight)
{
Empty();
DWORD paletteSize = pOldDib->NeedPalette() ?
sizeof(RGBQUAD) * pOldDib->UsedColors() : 0;
DWORD dibSize=sizeof(BITMAPINFOHEADER) + paletteSize;
if(bSizeChanged)
dibSize += (WIDTHBYTES(newWidth)*newHeight
*pOldDib->GetInfoHeaderPtr()->biBitCount)/sizeof(BYTE);
else
dibSize += pOldDib->ImageDataSize();
BYTE* pDib = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, dibSize);
m_pBmInfo = (LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
memcpy(m_pBmInfoHeader,pOldDib->m_pBmInfoHeader,
sizeof(BITMAPINFOHEADER));
if(bSizeChanged)
{
m_pBmInfoHeader->biWidth = newWidth;
m_pBmInfoHeader->biHeight = newHeight;
}
m_bNeedPalette = pOldDib->m_bNeedPalette;
if(m_bNeedPalette)
{
m_pRGBTable = (RGBQUAD*)(pDib+sizeof(BITMAPINFOHEADER));
memcpy(m_pRGBTable,pOldDib->m_pRGBTable,paletteSize );
m_hPalette = CreateDibPalette();
}
m_pBmBits = pDib + sizeof(BITMAPINFOHEADER) + paletteSize;
if(bSizeChanged)
memset(m_pBmBits,BYTE(255),ImageDataSize());
else
memcpy(m_pBmBits,pOldDib->m_pBmBits,pOldDib->ImageDataSize());
return TRUE;
}
#include "windows.h"
#include "windowsx.h"
#include "Dib.h"
CDib::CDib()
{
Initialize();
}
CDib::CDib(const char* fileName)
{
Initialize();
LoadFromFile(fileName);
}
void CDib::Initialize()
{
m_pBmInfo = 0;
m_pBmInfoHeader = 0;
m_pRGBTable = 0;
m_pBmBits = 0;
m_bNeedPalette = FALSE;
m_hPalette = NULL;
}
CDib::~CDib()
{
Empty();
}
//
void CDib::Empty()
{
if(m_pBmInfo)
GlobalFreePtr(m_pBmInfo); //
if(m_bNeedPalette && m_hPalette) // DeleteObject(m_hPalette);
}
//
BOOL CDib::LoadFromFile (LPCTSTR fileName)
{
CFile file;
//
if(file.Open(fileName, CFile::modeRead|CFile::shareDenyWrite)!=TRUE)
{
CString errorStr;
errorStr.Format(_T("´ò¿ª%s´íÎó"), (LPCTSTR)fileName);
// AfxMessageBox(errorStr);
//file.Close();
return FALSE;
}
//
BITMAPFILEHEADER bmFileHeader;
file.Read((void*)&bmFileHeader, sizeof(bmFileHeader));
m_BmFileHeader = bmFileHeader;
//
if (bmFileHeader.bfType != 0x4d42)
{
CString errorStr;
errorStr.Format("%s ²»ÊÇλͼÎļþ!!!", (LPCTSTR)fileName);
//AfxMessageBox(errorStr);
file.Close();
return FALSE;
}
DWORD fileLength = file.GetLength();
DWORD dibSize = fileLength - sizeof(bmFileHeader);
BYTE* pDib = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, dibSize);
file.Read((void*)pDib, dibSize);
file.Close();
m_pBmInfo = (LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
if (m_pBmInfoHeader->biSizeImage == 0)
m_pBmInfoHeader->biSizeImage
= m_pBmInfoHeader->biWidth * m_pBmInfoHeader->biHeight;
if(m_pBmInfoHeader->biClrUsed == 0)
m_pBmInfoHeader->biClrUsed = DWORD(1 << m_pBmInfoHeader->biBitCount);
if(m_pBmInfoHeader->biClrUsed <= 256)
{
m_bNeedPalette = TRUE;
m_pRGBTable = (RGBQUAD*)(pDib + m_pBmInfoHeader->biSize);
m_hPalette = CreateDibPalette();
}
DWORD clrTableSize = m_bNeedPalette ? UsedColors() * sizeof(RGBQUAD) : 0;
m_pBmBits = pDib + m_pBmInfoHeader->biSize + clrTableSize;
return TRUE;
}
BOOL CDib::SaveToFile(LPCTSTR fileName)
{
CFile file(fileName, CFile::modeWrite|CFile::modeCreate|CFile::shareDenyWrite);
BITMAPFILEHEADER bmFileHeader;
bmFileHeader.bfType = 0x4d42;
UINT height=Height();
UINT width=Width();
UINT line=WIDTHBYTES(width);
DWORD clrTableSize = m_bNeedPalette ? UsedColors() * sizeof(RGBQUAD) : 0;
DWORD dibInfoSize=m_pBmInfoHeader->biSize+clrTableSize;
DWORD dibSize=dibInfoSize+ImageDataSize();
bmFileHeader.bfSize = DWORD(dibSize+sizeof(BITMAPFILEHEADER));
bmFileHeader.bfReserved1 = bmFileHeader.bfReserved2 = 0;
bmFileHeader.bfOffBits = DWORD (dibInfoSize+sizeof(BITMAPFILEHEADER));
file.Write((void*)&bmFileHeader, sizeof(bmFileHeader));
file.WriteHuge((void*)m_pBmInfo, dibSize);
file.Close();
return TRUE;
}
BOOL CDib::CopyFromDib(CDib * pOldDib,BOOL bSizeChanged,
UINT newWidth, UINT newHeight)
{
Empty();
DWORD paletteSize = pOldDib->NeedPalette() ?
sizeof(RGBQUAD) * pOldDib->UsedColors() : 0;
DWORD dibSize=sizeof(BITMAPINFOHEADER) + paletteSize;
if(bSizeChanged)
dibSize += (WIDTHBYTES(newWidth)*newHeight
*pOldDib->GetInfoHeaderPtr()->biBitCount)/sizeof(BYTE);
else
dibSize += pOldDib->ImageDataSize();
BYTE* pDib = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, dibSize);
m_pBmInfo = (LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
memcpy(m_pBmInfoHeader,pOldDib->m_pBmInfoHeader,
sizeof(BITMAPINFOHEADER));
if(bSizeChanged)
{
m_pBmInfoHeader->biWidth = newWidth;
m_pBmInfoHeader->biHeight = newHeight;
}
m_bNeedPalette = pOldDib->m_bNeedPalette;
if(m_bNeedPalette)
{
m_pRGBTable = (RGBQUAD*)(pDib+sizeof(BITMAPINFOHEADER));
memcpy(m_pRGBTable,pOldDib->m_pRGBTable,paletteSize );
m_hPalette = CreateDibPalette();
}
m_pBmBits = pDib + sizeof(BITMAPINFOHEADER) + paletteSize;
if(bSizeChanged)
memset(m_pBmBits,BYTE(255),ImageDataSize());
else
memcpy(m_pBmBits,pOldDib->m_pBmBits,pOldDib->ImageDataSize());
return TRUE;
}
#3
int CDib::GetAt(int x,int y)
{
if((m_pBmInfo==NULL)||(x<0)||(x>=Width())||(y<0)||(y>=Height()))
return -1;
return m_pBmBits [ y*WIDTHBYTES(Width()) + x ];
}
int CDib::SetAt(int x,int y,int value)
{
if((m_pBmInfo==NULL)||(x<0)||(x>=Width())||(y<0)||(y>=Height())||(value>nMaxGray))
return -1;
m_pBmBits [ y*WIDTHBYTES(Width()) + x ] = value;
return 0;
}
DWORD CDib::ImageDataSize()
{
return GetLineSize()*Height();
}
HPALETTE CDib::CreateDibPalette()
{
//»ñµÃÑÕÉ«Êý
UINT numColors = UsedColors();
//¶¨ÒåÂß¼­µ÷É«°å
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
} logicalPalette = { 0x300, 256 };
//°ÑRGBTableÖеÄÊý¾ÝתÈëµ÷É«°åÖÐ
for(UINT i=0; i<numColors; ++i)
{
logicalPalette.aEntries[i].peRed = m_pRGBTable[i].rgbRed;
logicalPalette.aEntries[i].peGreen = m_pRGBTable[i].rgbGreen;
logicalPalette.aEntries[i].peBlue = m_pRGBTable[i].rgbBlue;
logicalPalette.aEntries[i].peFlags = 0;
}
//Éú³Éµ÷É«°å¾ä±ú
HPALETTE hPalette = ::CreatePalette((LPLOGPALETTE)&logicalPalette);
return hPalette;
}
//ÏÔʾÓÉCDib·â×°µÄλͼ£¬bStretchΪÊÇ·ñÀ­Éì
int CDib::ShowDib(CDC * pDC,CRect& rect,int bStretch)
{
BYTE* pBmBits = m_pBmBits;
LPBITMAPINFO pBmInfo = m_pBmInfo;
UINT bmWidth = Width();
UINT bmHeight = Height();
//¸ù¾ÝÊÇ·ñÀ­É죬¾ö¶¨ÏÔʾµÄ¾ØÐÎ
CRect drawRect;
if(bStretch==0)
drawRect = rect;
else if(bStretch==1)
{
drawRect.left = rect.left;
drawRect.top = rect.top;
drawRect.right = rect.left+bmWidth/2;
drawRect.bottom = rect.top+bmHeight/2;
}
else if(bStretch==2)
{
drawRect.left = rect.left;
drawRect.top = rect.top;
drawRect.right = rect.left+bmWidth/2*3;
drawRect.bottom = rect.top+bmHeight/2*3;
}
//Èç¹ûÐèÒªµ÷É«°å£¬Éú³Éµ÷É«°å
HPALETTE hPalette = NULL;
HPALETTE hOldPalette = NULL;
if(NeedPalette())
{
hPalette = GetDibPalette();
hOldPalette = SelectPalette(pDC->m_hDC,hPalette, FALSE);
RealizePalette(pDC->m_hDC);
}
//ʹÓÃStretchDIBitsÏÔʾ
::StretchDIBits(pDC->m_hDC,
drawRect.left,drawRect.top,drawRect.Width(),drawRect.Height(),
0, 0, bmWidth, bmHeight,
pBmBits, pBmInfo, DIB_RGB_COLORS, SRCCOPY);
//Êͷŵ÷É«°å
if(NeedPalette())
{
::SelectPalette(pDC->m_hDC,hOldPalette,FALSE);
::DeleteObject(hPalette);
}
return 0;
}
int CDib::GetLineSize()
{
int width = Width();
int nbits = GetBitCount() / 8;
width *= nbits;
return WIDTHBYTES(width); //·µ»ØÏñ¿íÖÐÆ«Ïò4µÄÖµ
}
BOOL CDib::EdgeEnhance(int Strength, int nAlgorithm)
{
// if (! EdgeEnhanceDIB(m_hDib, Strength, nAlgorithm))
// return FALSE;
return FALSE;
// return
// return UpdateInternal();
}
BOOL CDib::SetBmBitsPtr(BYTE *pDib)
{
m_pBmInfo=(LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
if (m_pBmInfoHeader->biSizeImage == 0)
m_pBmInfoHeader->biSizeImage = m_pBmInfoHeader->biWidth * m_pBmInfoHeader->biHeight;
if(m_pBmInfoHeader->biClrUsed == 0)
m_pBmInfoHeader->biClrUsed = DWORD(1 << m_pBmInfoHeader->biBitCount);
if(m_pBmInfoHeader->biClrUsed <= 256)
{
m_bNeedPalette = TRUE;
m_pRGBTable = (RGBQUAD*)(pDib + m_pBmInfoHeader->biSize);
m_hPalette = CreateDibPalette();
}
m_pBmBits=pDib+m_pBmInfoHeader->biSize;
return true;
}
{
if((m_pBmInfo==NULL)||(x<0)||(x>=Width())||(y<0)||(y>=Height()))
return -1;
return m_pBmBits [ y*WIDTHBYTES(Width()) + x ];
}
int CDib::SetAt(int x,int y,int value)
{
if((m_pBmInfo==NULL)||(x<0)||(x>=Width())||(y<0)||(y>=Height())||(value>nMaxGray))
return -1;
m_pBmBits [ y*WIDTHBYTES(Width()) + x ] = value;
return 0;
}
DWORD CDib::ImageDataSize()
{
return GetLineSize()*Height();
}
HPALETTE CDib::CreateDibPalette()
{
//»ñµÃÑÕÉ«Êý
UINT numColors = UsedColors();
//¶¨ÒåÂß¼­µ÷É«°å
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
} logicalPalette = { 0x300, 256 };
//°ÑRGBTableÖеÄÊý¾ÝתÈëµ÷É«°åÖÐ
for(UINT i=0; i<numColors; ++i)
{
logicalPalette.aEntries[i].peRed = m_pRGBTable[i].rgbRed;
logicalPalette.aEntries[i].peGreen = m_pRGBTable[i].rgbGreen;
logicalPalette.aEntries[i].peBlue = m_pRGBTable[i].rgbBlue;
logicalPalette.aEntries[i].peFlags = 0;
}
//Éú³Éµ÷É«°å¾ä±ú
HPALETTE hPalette = ::CreatePalette((LPLOGPALETTE)&logicalPalette);
return hPalette;
}
//ÏÔʾÓÉCDib·â×°µÄλͼ£¬bStretchΪÊÇ·ñÀ­Éì
int CDib::ShowDib(CDC * pDC,CRect& rect,int bStretch)
{
BYTE* pBmBits = m_pBmBits;
LPBITMAPINFO pBmInfo = m_pBmInfo;
UINT bmWidth = Width();
UINT bmHeight = Height();
//¸ù¾ÝÊÇ·ñÀ­É죬¾ö¶¨ÏÔʾµÄ¾ØÐÎ
CRect drawRect;
if(bStretch==0)
drawRect = rect;
else if(bStretch==1)
{
drawRect.left = rect.left;
drawRect.top = rect.top;
drawRect.right = rect.left+bmWidth/2;
drawRect.bottom = rect.top+bmHeight/2;
}
else if(bStretch==2)
{
drawRect.left = rect.left;
drawRect.top = rect.top;
drawRect.right = rect.left+bmWidth/2*3;
drawRect.bottom = rect.top+bmHeight/2*3;
}
//Èç¹ûÐèÒªµ÷É«°å£¬Éú³Éµ÷É«°å
HPALETTE hPalette = NULL;
HPALETTE hOldPalette = NULL;
if(NeedPalette())
{
hPalette = GetDibPalette();
hOldPalette = SelectPalette(pDC->m_hDC,hPalette, FALSE);
RealizePalette(pDC->m_hDC);
}
//ʹÓÃStretchDIBitsÏÔʾ
::StretchDIBits(pDC->m_hDC,
drawRect.left,drawRect.top,drawRect.Width(),drawRect.Height(),
0, 0, bmWidth, bmHeight,
pBmBits, pBmInfo, DIB_RGB_COLORS, SRCCOPY);
//Êͷŵ÷É«°å
if(NeedPalette())
{
::SelectPalette(pDC->m_hDC,hOldPalette,FALSE);
::DeleteObject(hPalette);
}
return 0;
}
int CDib::GetLineSize()
{
int width = Width();
int nbits = GetBitCount() / 8;
width *= nbits;
return WIDTHBYTES(width); //·µ»ØÏñ¿íÖÐÆ«Ïò4µÄÖµ
}
BOOL CDib::EdgeEnhance(int Strength, int nAlgorithm)
{
// if (! EdgeEnhanceDIB(m_hDib, Strength, nAlgorithm))
// return FALSE;
return FALSE;
// return
// return UpdateInternal();
}
BOOL CDib::SetBmBitsPtr(BYTE *pDib)
{
m_pBmInfo=(LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
if (m_pBmInfoHeader->biSizeImage == 0)
m_pBmInfoHeader->biSizeImage = m_pBmInfoHeader->biWidth * m_pBmInfoHeader->biHeight;
if(m_pBmInfoHeader->biClrUsed == 0)
m_pBmInfoHeader->biClrUsed = DWORD(1 << m_pBmInfoHeader->biBitCount);
if(m_pBmInfoHeader->biClrUsed <= 256)
{
m_bNeedPalette = TRUE;
m_pRGBTable = (RGBQUAD*)(pDib + m_pBmInfoHeader->biSize);
m_hPalette = CreateDibPalette();
}
m_pBmBits=pDib+m_pBmInfoHeader->biSize;
return true;
}
#4
你直接定义那几个个结构类型的变量就行了
操作的话,封装的函数就够用了.
你要是想用API自己做根本就没必要CBitmap *pBitmap=CBitmap::FromHandle(hBitmap)
操作的话,封装的函数就够用了.
你要是想用API自己做根本就没必要CBitmap *pBitmap=CBitmap::FromHandle(hBitmap)
#5
不給分都不行了.
#1
还不如做个自己封装得类
下面给你一个封装得类
#ifndef _DIB_H
#define _DIB_H
const int nMaxGray=255;
inline int WIDTHBYTES(int i) { return (i%4)? (i-i%4+4) : i ; }
//ÓÃÓÚÏÔʾBMPµÄCDibÀà
class CDib: public CObject
{
public:
int GetLineSize();
int GetBitCount () { return m_pBmInfoHeader->biBitCount; }
CDib();
CDib(const char* fileName); //ÒÔÎļþÃûΪ²ÎÊýµÄ¹¹Ô캯Êý
~CDib();
BOOL CopyFromDib(CDib * pOldDib,BOOL sizeChanged=FALSE,
UINT newWidth=0, UINT newHeight=0); //¿½±´Î»Í¼
BOOL LoadFromFile(LPCTSTR fileName); //´ÓÎļþ¶ÁÈëλͼ
BOOL SaveToFile(LPCTSTR fileName); //°ÑλͼдÈëÎļþ
int GetAt(int x,int y); //»ñµÃÖ¸¶¨µãµÄÑÕÉ«Öµ
int SetAt(int x,int y,int value); //ÉèÖÃÖ¸¶¨µãµÄÑÕÉ«Öµ
//»ñµÃλͼµÄ¿í¶È
int Width() { return m_pBmInfoHeader->biWidth; }
//»ñµÃλͼµÄ¸ß¶È
int Height() { return m_pBmInfoHeader->biHeight; }
//»ñµÃλͼµÄʵ¼Ê´óС
DWORD ImageSize() { return m_pBmInfoHeader->biSizeImage; }
//»ñµÃλͼʹÓõÄÑÕÉ«Êý
DWORD UsedColors(){ return (DWORD) m_pBmInfoHeader->biClrUsed; }
DWORD BMPSize(){ return (DWORD)m_BmFileHeader.bfSize;}
DWORD ImageDataSize(); //¼ÆËãͼÏñÊý¾Ý×Ö½ÚÊý
//»ñµÃÐÅϢͷָÕë
LPBITMAPINFOHEADER GetInfoHeaderPtr() { return m_pBmInfoHeader; }
//»ñµÃÐÅÏ¢Ö¸Õë
LPBITMAPINFO GetInfoPtr() { return m_pBmInfo; }
//»ñµÃÑÕÉ«²éÕÒ±íÖ¸Õë
LPRGBQUAD GetRGBTablePtr() { return m_pRGBTable; }
//»ñµÃµãÊý×éµÄÖ¸Õë
BYTE * GetBmBitsPtr() { return m_pBmBits; }
//·µ»ØÊÇ·ñÐèÒªµ÷É«°å
BOOL NeedPalette() { return m_bNeedPalette; }
//·µ»ØλͼµÄµ÷É«°å
HPALETTE GetDibPalette() { return m_hPalette; }
//ÏÔʾÓÉCDib·â×°µÄλͼ
int ShowDib(CDC * pDC,CRect& rect,int bStretch);
BOOL EdgeEnhance(int Strength, int nAlgorithm);
//private:
public:
void Initialize(); //³õʼ»¯º¯Êý
void Empty(); //Çå¿Õº¯Êý
HPALETTE CreateDibPalette(); //´´½¨µ÷É«°å£¬·µ»ØÆä¾ä±ú
//private:
public:
BOOL SetBmBitsPtr(BYTE * pDib);
BITMAPFILEHEADER m_BmFileHeader;
LPBITMAPINFO m_pBmInfo; //λͼÐÅÏ¢Ö¸Õë
LPBITMAPINFOHEADER m_pBmInfoHeader; //λͼÐÅϢͷָÕë
RGBQUAD * m_pRGBTable; //λͼÑÕÉ«²éÕÒ±íÖ¸Õë
BYTE * m_pBmBits; //λͼµãÊý×é
BOOL m_bNeedPalette; //λͼÊÇ·ñÐèÒªµ÷É«°å
HPALETTE m_hPalette; //λͼµÄµ÷É«°å
};
#endif
下面给你一个封装得类
#ifndef _DIB_H
#define _DIB_H
const int nMaxGray=255;
inline int WIDTHBYTES(int i) { return (i%4)? (i-i%4+4) : i ; }
//ÓÃÓÚÏÔʾBMPµÄCDibÀà
class CDib: public CObject
{
public:
int GetLineSize();
int GetBitCount () { return m_pBmInfoHeader->biBitCount; }
CDib();
CDib(const char* fileName); //ÒÔÎļþÃûΪ²ÎÊýµÄ¹¹Ô캯Êý
~CDib();
BOOL CopyFromDib(CDib * pOldDib,BOOL sizeChanged=FALSE,
UINT newWidth=0, UINT newHeight=0); //¿½±´Î»Í¼
BOOL LoadFromFile(LPCTSTR fileName); //´ÓÎļþ¶ÁÈëλͼ
BOOL SaveToFile(LPCTSTR fileName); //°ÑλͼдÈëÎļþ
int GetAt(int x,int y); //»ñµÃÖ¸¶¨µãµÄÑÕÉ«Öµ
int SetAt(int x,int y,int value); //ÉèÖÃÖ¸¶¨µãµÄÑÕÉ«Öµ
//»ñµÃλͼµÄ¿í¶È
int Width() { return m_pBmInfoHeader->biWidth; }
//»ñµÃλͼµÄ¸ß¶È
int Height() { return m_pBmInfoHeader->biHeight; }
//»ñµÃλͼµÄʵ¼Ê´óС
DWORD ImageSize() { return m_pBmInfoHeader->biSizeImage; }
//»ñµÃλͼʹÓõÄÑÕÉ«Êý
DWORD UsedColors(){ return (DWORD) m_pBmInfoHeader->biClrUsed; }
DWORD BMPSize(){ return (DWORD)m_BmFileHeader.bfSize;}
DWORD ImageDataSize(); //¼ÆËãͼÏñÊý¾Ý×Ö½ÚÊý
//»ñµÃÐÅϢͷָÕë
LPBITMAPINFOHEADER GetInfoHeaderPtr() { return m_pBmInfoHeader; }
//»ñµÃÐÅÏ¢Ö¸Õë
LPBITMAPINFO GetInfoPtr() { return m_pBmInfo; }
//»ñµÃÑÕÉ«²éÕÒ±íÖ¸Õë
LPRGBQUAD GetRGBTablePtr() { return m_pRGBTable; }
//»ñµÃµãÊý×éµÄÖ¸Õë
BYTE * GetBmBitsPtr() { return m_pBmBits; }
//·µ»ØÊÇ·ñÐèÒªµ÷É«°å
BOOL NeedPalette() { return m_bNeedPalette; }
//·µ»ØλͼµÄµ÷É«°å
HPALETTE GetDibPalette() { return m_hPalette; }
//ÏÔʾÓÉCDib·â×°µÄλͼ
int ShowDib(CDC * pDC,CRect& rect,int bStretch);
BOOL EdgeEnhance(int Strength, int nAlgorithm);
//private:
public:
void Initialize(); //³õʼ»¯º¯Êý
void Empty(); //Çå¿Õº¯Êý
HPALETTE CreateDibPalette(); //´´½¨µ÷É«°å£¬·µ»ØÆä¾ä±ú
//private:
public:
BOOL SetBmBitsPtr(BYTE * pDib);
BITMAPFILEHEADER m_BmFileHeader;
LPBITMAPINFO m_pBmInfo; //λͼÐÅÏ¢Ö¸Õë
LPBITMAPINFOHEADER m_pBmInfoHeader; //λͼÐÅϢͷָÕë
RGBQUAD * m_pRGBTable; //λͼÑÕÉ«²éÕÒ±íÖ¸Õë
BYTE * m_pBmBits; //λͼµãÊý×é
BOOL m_bNeedPalette; //λͼÊÇ·ñÐèÒªµ÷É«°å
HPALETTE m_hPalette; //λͼµÄµ÷É«°å
};
#endif
#2
#include "stdafx.h"
#include "windows.h"
#include "windowsx.h"
#include "Dib.h"
CDib::CDib()
{
Initialize();
}
CDib::CDib(const char* fileName)
{
Initialize();
LoadFromFile(fileName);
}
void CDib::Initialize()
{
m_pBmInfo = 0;
m_pBmInfoHeader = 0;
m_pRGBTable = 0;
m_pBmBits = 0;
m_bNeedPalette = FALSE;
m_hPalette = NULL;
}
CDib::~CDib()
{
Empty();
}
//
void CDib::Empty()
{
if(m_pBmInfo)
GlobalFreePtr(m_pBmInfo); //
if(m_bNeedPalette && m_hPalette) // DeleteObject(m_hPalette);
}
//
BOOL CDib::LoadFromFile (LPCTSTR fileName)
{
CFile file;
//
if(file.Open(fileName, CFile::modeRead|CFile::shareDenyWrite)!=TRUE)
{
CString errorStr;
errorStr.Format(_T("´ò¿ª%s´íÎó"), (LPCTSTR)fileName);
// AfxMessageBox(errorStr);
//file.Close();
return FALSE;
}
//
BITMAPFILEHEADER bmFileHeader;
file.Read((void*)&bmFileHeader, sizeof(bmFileHeader));
m_BmFileHeader = bmFileHeader;
//
if (bmFileHeader.bfType != 0x4d42)
{
CString errorStr;
errorStr.Format("%s ²»ÊÇλͼÎļþ!!!", (LPCTSTR)fileName);
//AfxMessageBox(errorStr);
file.Close();
return FALSE;
}
DWORD fileLength = file.GetLength();
DWORD dibSize = fileLength - sizeof(bmFileHeader);
BYTE* pDib = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, dibSize);
file.Read((void*)pDib, dibSize);
file.Close();
m_pBmInfo = (LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
if (m_pBmInfoHeader->biSizeImage == 0)
m_pBmInfoHeader->biSizeImage
= m_pBmInfoHeader->biWidth * m_pBmInfoHeader->biHeight;
if(m_pBmInfoHeader->biClrUsed == 0)
m_pBmInfoHeader->biClrUsed = DWORD(1 << m_pBmInfoHeader->biBitCount);
if(m_pBmInfoHeader->biClrUsed <= 256)
{
m_bNeedPalette = TRUE;
m_pRGBTable = (RGBQUAD*)(pDib + m_pBmInfoHeader->biSize);
m_hPalette = CreateDibPalette();
}
DWORD clrTableSize = m_bNeedPalette ? UsedColors() * sizeof(RGBQUAD) : 0;
m_pBmBits = pDib + m_pBmInfoHeader->biSize + clrTableSize;
return TRUE;
}
BOOL CDib::SaveToFile(LPCTSTR fileName)
{
CFile file(fileName, CFile::modeWrite|CFile::modeCreate|CFile::shareDenyWrite);
BITMAPFILEHEADER bmFileHeader;
bmFileHeader.bfType = 0x4d42;
UINT height=Height();
UINT width=Width();
UINT line=WIDTHBYTES(width);
DWORD clrTableSize = m_bNeedPalette ? UsedColors() * sizeof(RGBQUAD) : 0;
DWORD dibInfoSize=m_pBmInfoHeader->biSize+clrTableSize;
DWORD dibSize=dibInfoSize+ImageDataSize();
bmFileHeader.bfSize = DWORD(dibSize+sizeof(BITMAPFILEHEADER));
bmFileHeader.bfReserved1 = bmFileHeader.bfReserved2 = 0;
bmFileHeader.bfOffBits = DWORD (dibInfoSize+sizeof(BITMAPFILEHEADER));
file.Write((void*)&bmFileHeader, sizeof(bmFileHeader));
file.WriteHuge((void*)m_pBmInfo, dibSize);
file.Close();
return TRUE;
}
BOOL CDib::CopyFromDib(CDib * pOldDib,BOOL bSizeChanged,
UINT newWidth, UINT newHeight)
{
Empty();
DWORD paletteSize = pOldDib->NeedPalette() ?
sizeof(RGBQUAD) * pOldDib->UsedColors() : 0;
DWORD dibSize=sizeof(BITMAPINFOHEADER) + paletteSize;
if(bSizeChanged)
dibSize += (WIDTHBYTES(newWidth)*newHeight
*pOldDib->GetInfoHeaderPtr()->biBitCount)/sizeof(BYTE);
else
dibSize += pOldDib->ImageDataSize();
BYTE* pDib = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, dibSize);
m_pBmInfo = (LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
memcpy(m_pBmInfoHeader,pOldDib->m_pBmInfoHeader,
sizeof(BITMAPINFOHEADER));
if(bSizeChanged)
{
m_pBmInfoHeader->biWidth = newWidth;
m_pBmInfoHeader->biHeight = newHeight;
}
m_bNeedPalette = pOldDib->m_bNeedPalette;
if(m_bNeedPalette)
{
m_pRGBTable = (RGBQUAD*)(pDib+sizeof(BITMAPINFOHEADER));
memcpy(m_pRGBTable,pOldDib->m_pRGBTable,paletteSize );
m_hPalette = CreateDibPalette();
}
m_pBmBits = pDib + sizeof(BITMAPINFOHEADER) + paletteSize;
if(bSizeChanged)
memset(m_pBmBits,BYTE(255),ImageDataSize());
else
memcpy(m_pBmBits,pOldDib->m_pBmBits,pOldDib->ImageDataSize());
return TRUE;
}
#include "windows.h"
#include "windowsx.h"
#include "Dib.h"
CDib::CDib()
{
Initialize();
}
CDib::CDib(const char* fileName)
{
Initialize();
LoadFromFile(fileName);
}
void CDib::Initialize()
{
m_pBmInfo = 0;
m_pBmInfoHeader = 0;
m_pRGBTable = 0;
m_pBmBits = 0;
m_bNeedPalette = FALSE;
m_hPalette = NULL;
}
CDib::~CDib()
{
Empty();
}
//
void CDib::Empty()
{
if(m_pBmInfo)
GlobalFreePtr(m_pBmInfo); //
if(m_bNeedPalette && m_hPalette) // DeleteObject(m_hPalette);
}
//
BOOL CDib::LoadFromFile (LPCTSTR fileName)
{
CFile file;
//
if(file.Open(fileName, CFile::modeRead|CFile::shareDenyWrite)!=TRUE)
{
CString errorStr;
errorStr.Format(_T("´ò¿ª%s´íÎó"), (LPCTSTR)fileName);
// AfxMessageBox(errorStr);
//file.Close();
return FALSE;
}
//
BITMAPFILEHEADER bmFileHeader;
file.Read((void*)&bmFileHeader, sizeof(bmFileHeader));
m_BmFileHeader = bmFileHeader;
//
if (bmFileHeader.bfType != 0x4d42)
{
CString errorStr;
errorStr.Format("%s ²»ÊÇλͼÎļþ!!!", (LPCTSTR)fileName);
//AfxMessageBox(errorStr);
file.Close();
return FALSE;
}
DWORD fileLength = file.GetLength();
DWORD dibSize = fileLength - sizeof(bmFileHeader);
BYTE* pDib = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, dibSize);
file.Read((void*)pDib, dibSize);
file.Close();
m_pBmInfo = (LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
if (m_pBmInfoHeader->biSizeImage == 0)
m_pBmInfoHeader->biSizeImage
= m_pBmInfoHeader->biWidth * m_pBmInfoHeader->biHeight;
if(m_pBmInfoHeader->biClrUsed == 0)
m_pBmInfoHeader->biClrUsed = DWORD(1 << m_pBmInfoHeader->biBitCount);
if(m_pBmInfoHeader->biClrUsed <= 256)
{
m_bNeedPalette = TRUE;
m_pRGBTable = (RGBQUAD*)(pDib + m_pBmInfoHeader->biSize);
m_hPalette = CreateDibPalette();
}
DWORD clrTableSize = m_bNeedPalette ? UsedColors() * sizeof(RGBQUAD) : 0;
m_pBmBits = pDib + m_pBmInfoHeader->biSize + clrTableSize;
return TRUE;
}
BOOL CDib::SaveToFile(LPCTSTR fileName)
{
CFile file(fileName, CFile::modeWrite|CFile::modeCreate|CFile::shareDenyWrite);
BITMAPFILEHEADER bmFileHeader;
bmFileHeader.bfType = 0x4d42;
UINT height=Height();
UINT width=Width();
UINT line=WIDTHBYTES(width);
DWORD clrTableSize = m_bNeedPalette ? UsedColors() * sizeof(RGBQUAD) : 0;
DWORD dibInfoSize=m_pBmInfoHeader->biSize+clrTableSize;
DWORD dibSize=dibInfoSize+ImageDataSize();
bmFileHeader.bfSize = DWORD(dibSize+sizeof(BITMAPFILEHEADER));
bmFileHeader.bfReserved1 = bmFileHeader.bfReserved2 = 0;
bmFileHeader.bfOffBits = DWORD (dibInfoSize+sizeof(BITMAPFILEHEADER));
file.Write((void*)&bmFileHeader, sizeof(bmFileHeader));
file.WriteHuge((void*)m_pBmInfo, dibSize);
file.Close();
return TRUE;
}
BOOL CDib::CopyFromDib(CDib * pOldDib,BOOL bSizeChanged,
UINT newWidth, UINT newHeight)
{
Empty();
DWORD paletteSize = pOldDib->NeedPalette() ?
sizeof(RGBQUAD) * pOldDib->UsedColors() : 0;
DWORD dibSize=sizeof(BITMAPINFOHEADER) + paletteSize;
if(bSizeChanged)
dibSize += (WIDTHBYTES(newWidth)*newHeight
*pOldDib->GetInfoHeaderPtr()->biBitCount)/sizeof(BYTE);
else
dibSize += pOldDib->ImageDataSize();
BYTE* pDib = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, dibSize);
m_pBmInfo = (LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
memcpy(m_pBmInfoHeader,pOldDib->m_pBmInfoHeader,
sizeof(BITMAPINFOHEADER));
if(bSizeChanged)
{
m_pBmInfoHeader->biWidth = newWidth;
m_pBmInfoHeader->biHeight = newHeight;
}
m_bNeedPalette = pOldDib->m_bNeedPalette;
if(m_bNeedPalette)
{
m_pRGBTable = (RGBQUAD*)(pDib+sizeof(BITMAPINFOHEADER));
memcpy(m_pRGBTable,pOldDib->m_pRGBTable,paletteSize );
m_hPalette = CreateDibPalette();
}
m_pBmBits = pDib + sizeof(BITMAPINFOHEADER) + paletteSize;
if(bSizeChanged)
memset(m_pBmBits,BYTE(255),ImageDataSize());
else
memcpy(m_pBmBits,pOldDib->m_pBmBits,pOldDib->ImageDataSize());
return TRUE;
}
#3
int CDib::GetAt(int x,int y)
{
if((m_pBmInfo==NULL)||(x<0)||(x>=Width())||(y<0)||(y>=Height()))
return -1;
return m_pBmBits [ y*WIDTHBYTES(Width()) + x ];
}
int CDib::SetAt(int x,int y,int value)
{
if((m_pBmInfo==NULL)||(x<0)||(x>=Width())||(y<0)||(y>=Height())||(value>nMaxGray))
return -1;
m_pBmBits [ y*WIDTHBYTES(Width()) + x ] = value;
return 0;
}
DWORD CDib::ImageDataSize()
{
return GetLineSize()*Height();
}
HPALETTE CDib::CreateDibPalette()
{
//»ñµÃÑÕÉ«Êý
UINT numColors = UsedColors();
//¶¨ÒåÂß¼­µ÷É«°å
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
} logicalPalette = { 0x300, 256 };
//°ÑRGBTableÖеÄÊý¾ÝתÈëµ÷É«°åÖÐ
for(UINT i=0; i<numColors; ++i)
{
logicalPalette.aEntries[i].peRed = m_pRGBTable[i].rgbRed;
logicalPalette.aEntries[i].peGreen = m_pRGBTable[i].rgbGreen;
logicalPalette.aEntries[i].peBlue = m_pRGBTable[i].rgbBlue;
logicalPalette.aEntries[i].peFlags = 0;
}
//Éú³Éµ÷É«°å¾ä±ú
HPALETTE hPalette = ::CreatePalette((LPLOGPALETTE)&logicalPalette);
return hPalette;
}
//ÏÔʾÓÉCDib·â×°µÄλͼ£¬bStretchΪÊÇ·ñÀ­Éì
int CDib::ShowDib(CDC * pDC,CRect& rect,int bStretch)
{
BYTE* pBmBits = m_pBmBits;
LPBITMAPINFO pBmInfo = m_pBmInfo;
UINT bmWidth = Width();
UINT bmHeight = Height();
//¸ù¾ÝÊÇ·ñÀ­É죬¾ö¶¨ÏÔʾµÄ¾ØÐÎ
CRect drawRect;
if(bStretch==0)
drawRect = rect;
else if(bStretch==1)
{
drawRect.left = rect.left;
drawRect.top = rect.top;
drawRect.right = rect.left+bmWidth/2;
drawRect.bottom = rect.top+bmHeight/2;
}
else if(bStretch==2)
{
drawRect.left = rect.left;
drawRect.top = rect.top;
drawRect.right = rect.left+bmWidth/2*3;
drawRect.bottom = rect.top+bmHeight/2*3;
}
//Èç¹ûÐèÒªµ÷É«°å£¬Éú³Éµ÷É«°å
HPALETTE hPalette = NULL;
HPALETTE hOldPalette = NULL;
if(NeedPalette())
{
hPalette = GetDibPalette();
hOldPalette = SelectPalette(pDC->m_hDC,hPalette, FALSE);
RealizePalette(pDC->m_hDC);
}
//ʹÓÃStretchDIBitsÏÔʾ
::StretchDIBits(pDC->m_hDC,
drawRect.left,drawRect.top,drawRect.Width(),drawRect.Height(),
0, 0, bmWidth, bmHeight,
pBmBits, pBmInfo, DIB_RGB_COLORS, SRCCOPY);
//Êͷŵ÷É«°å
if(NeedPalette())
{
::SelectPalette(pDC->m_hDC,hOldPalette,FALSE);
::DeleteObject(hPalette);
}
return 0;
}
int CDib::GetLineSize()
{
int width = Width();
int nbits = GetBitCount() / 8;
width *= nbits;
return WIDTHBYTES(width); //·µ»ØÏñ¿íÖÐÆ«Ïò4µÄÖµ
}
BOOL CDib::EdgeEnhance(int Strength, int nAlgorithm)
{
// if (! EdgeEnhanceDIB(m_hDib, Strength, nAlgorithm))
// return FALSE;
return FALSE;
// return
// return UpdateInternal();
}
BOOL CDib::SetBmBitsPtr(BYTE *pDib)
{
m_pBmInfo=(LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
if (m_pBmInfoHeader->biSizeImage == 0)
m_pBmInfoHeader->biSizeImage = m_pBmInfoHeader->biWidth * m_pBmInfoHeader->biHeight;
if(m_pBmInfoHeader->biClrUsed == 0)
m_pBmInfoHeader->biClrUsed = DWORD(1 << m_pBmInfoHeader->biBitCount);
if(m_pBmInfoHeader->biClrUsed <= 256)
{
m_bNeedPalette = TRUE;
m_pRGBTable = (RGBQUAD*)(pDib + m_pBmInfoHeader->biSize);
m_hPalette = CreateDibPalette();
}
m_pBmBits=pDib+m_pBmInfoHeader->biSize;
return true;
}
{
if((m_pBmInfo==NULL)||(x<0)||(x>=Width())||(y<0)||(y>=Height()))
return -1;
return m_pBmBits [ y*WIDTHBYTES(Width()) + x ];
}
int CDib::SetAt(int x,int y,int value)
{
if((m_pBmInfo==NULL)||(x<0)||(x>=Width())||(y<0)||(y>=Height())||(value>nMaxGray))
return -1;
m_pBmBits [ y*WIDTHBYTES(Width()) + x ] = value;
return 0;
}
DWORD CDib::ImageDataSize()
{
return GetLineSize()*Height();
}
HPALETTE CDib::CreateDibPalette()
{
//»ñµÃÑÕÉ«Êý
UINT numColors = UsedColors();
//¶¨ÒåÂß¼­µ÷É«°å
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
} logicalPalette = { 0x300, 256 };
//°ÑRGBTableÖеÄÊý¾ÝתÈëµ÷É«°åÖÐ
for(UINT i=0; i<numColors; ++i)
{
logicalPalette.aEntries[i].peRed = m_pRGBTable[i].rgbRed;
logicalPalette.aEntries[i].peGreen = m_pRGBTable[i].rgbGreen;
logicalPalette.aEntries[i].peBlue = m_pRGBTable[i].rgbBlue;
logicalPalette.aEntries[i].peFlags = 0;
}
//Éú³Éµ÷É«°å¾ä±ú
HPALETTE hPalette = ::CreatePalette((LPLOGPALETTE)&logicalPalette);
return hPalette;
}
//ÏÔʾÓÉCDib·â×°µÄλͼ£¬bStretchΪÊÇ·ñÀ­Éì
int CDib::ShowDib(CDC * pDC,CRect& rect,int bStretch)
{
BYTE* pBmBits = m_pBmBits;
LPBITMAPINFO pBmInfo = m_pBmInfo;
UINT bmWidth = Width();
UINT bmHeight = Height();
//¸ù¾ÝÊÇ·ñÀ­É죬¾ö¶¨ÏÔʾµÄ¾ØÐÎ
CRect drawRect;
if(bStretch==0)
drawRect = rect;
else if(bStretch==1)
{
drawRect.left = rect.left;
drawRect.top = rect.top;
drawRect.right = rect.left+bmWidth/2;
drawRect.bottom = rect.top+bmHeight/2;
}
else if(bStretch==2)
{
drawRect.left = rect.left;
drawRect.top = rect.top;
drawRect.right = rect.left+bmWidth/2*3;
drawRect.bottom = rect.top+bmHeight/2*3;
}
//Èç¹ûÐèÒªµ÷É«°å£¬Éú³Éµ÷É«°å
HPALETTE hPalette = NULL;
HPALETTE hOldPalette = NULL;
if(NeedPalette())
{
hPalette = GetDibPalette();
hOldPalette = SelectPalette(pDC->m_hDC,hPalette, FALSE);
RealizePalette(pDC->m_hDC);
}
//ʹÓÃStretchDIBitsÏÔʾ
::StretchDIBits(pDC->m_hDC,
drawRect.left,drawRect.top,drawRect.Width(),drawRect.Height(),
0, 0, bmWidth, bmHeight,
pBmBits, pBmInfo, DIB_RGB_COLORS, SRCCOPY);
//Êͷŵ÷É«°å
if(NeedPalette())
{
::SelectPalette(pDC->m_hDC,hOldPalette,FALSE);
::DeleteObject(hPalette);
}
return 0;
}
int CDib::GetLineSize()
{
int width = Width();
int nbits = GetBitCount() / 8;
width *= nbits;
return WIDTHBYTES(width); //·µ»ØÏñ¿íÖÐÆ«Ïò4µÄÖµ
}
BOOL CDib::EdgeEnhance(int Strength, int nAlgorithm)
{
// if (! EdgeEnhanceDIB(m_hDib, Strength, nAlgorithm))
// return FALSE;
return FALSE;
// return
// return UpdateInternal();
}
BOOL CDib::SetBmBitsPtr(BYTE *pDib)
{
m_pBmInfo=(LPBITMAPINFO) pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER) pDib;
if (m_pBmInfoHeader->biSizeImage == 0)
m_pBmInfoHeader->biSizeImage = m_pBmInfoHeader->biWidth * m_pBmInfoHeader->biHeight;
if(m_pBmInfoHeader->biClrUsed == 0)
m_pBmInfoHeader->biClrUsed = DWORD(1 << m_pBmInfoHeader->biBitCount);
if(m_pBmInfoHeader->biClrUsed <= 256)
{
m_bNeedPalette = TRUE;
m_pRGBTable = (RGBQUAD*)(pDib + m_pBmInfoHeader->biSize);
m_hPalette = CreateDibPalette();
}
m_pBmBits=pDib+m_pBmInfoHeader->biSize;
return true;
}
#4
你直接定义那几个个结构类型的变量就行了
操作的话,封装的函数就够用了.
你要是想用API自己做根本就没必要CBitmap *pBitmap=CBitmap::FromHandle(hBitmap)
操作的话,封装的函数就够用了.
你要是想用API自己做根本就没必要CBitmap *pBitmap=CBitmap::FromHandle(hBitmap)
#5
不給分都不行了.