Bitmap class

wavsyntax 0 Tallied Votes 262 Views Share

Enjoy.

[/code]
#define MAXWRITE (1024*16)

class Bitmap{
 private:
 //This function is slow, and thus not good for animation purposes
 //but for displaying a transparent bitmap that doesn't get redrawn  
 //constantly this is a good function
 void DrawTransparentBitmap(HDC hdc,HBITMAP hbm,int ix=0,int iy=0,COLORREF transClr=RGB(255,255,255)){
 BITMAP bm;  
 int tempx=ix,tempy=iy;
 GetObject(hbm,sizeof(bm),&bm);
 HDC hdc2=CreateCompatibleDC(hdc);
 SelectObject(hdc2,hbm);
 int xx=0,yy=0;
  for(int y=tempy;y<tempy+bm.bmHeight;y++){
   for(int x=tempx;x<tempx+bm.bmWidth;x++){
   COLORREF clr=GetPixel(hdc2,xx,yy);
    if(clr!=transClr){
    SetPixel(hdc,x,y,clr);
    }
   xx++;
   }
  xx=0;
  yy++;
  }
 DeleteDC(hdc2);
 }
 

 //I added this function from win32 api.hlp, I plan to write my own some day
 PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp) {  
 BITMAP bmp; 
 PBITMAPINFO pbmi; 
 WORD cClrBits; 
 if(!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp)){
 MessageBox(0,"Bitmap failed to save","Error...",MB_OK);                     
 } 
 cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); 
  if(cClrBits == 1){ 
  cClrBits = 1; 
  }
  else if(cClrBits <= 4){ 
  cClrBits = 4; 
  }
  else if(cClrBits <= 8){ 
  cClrBits = 8; 
  }
  else if(cClrBits <= 16){ 
  cClrBits = 16; 
  }
  else if(cClrBits <= 24){ 
  cClrBits = 24; 
  }
  else{ 
  cClrBits = 32;
  } 
  if(cClrBits != 24){
  pbmi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD) * (2^cClrBits)); 
  }
  else{
  pbmi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)); 
  }
 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
 pbmi->bmiHeader.biWidth = bmp.bmWidth; 
 pbmi->bmiHeader.biHeight = bmp.bmHeight; 
 pbmi->bmiHeader.biPlanes = bmp.bmPlanes; 
 pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; 
  if(cClrBits < 24){ 
  pbmi->bmiHeader.biClrUsed = 2^cClrBits;
  } 
 pbmi->bmiHeader.biCompression = BI_RGB; 
 pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8 
                                  * pbmi->bmiHeader.biHeight 
                                  * cClrBits; 
 pbmi->bmiHeader.biClrImportant = 0; 
 return pbmi;  
 } 


 //I added this function from win32 api.hlp, I plan to write my own some day
 void CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP,HDC hDC){
 HANDLE hf;                
 BITMAPFILEHEADER hdr;     
 PBITMAPINFOHEADER pbih;    
 LPBYTE lpBits;           
 DWORD dwTotal; 
 DWORD cb;  
 BYTE *hp;
 DWORD dwTmp; 
 pbih = (PBITMAPINFOHEADER) pbi; 
 lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
  if(!lpBits){
  MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);            
  }
  if(!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS)){
  MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);                           
  }
 hf=CreateFile(pszFile, 
                   GENERIC_READ | GENERIC_WRITE, 
                   (DWORD) 0, 
                   (LPSECURITY_ATTRIBUTES) NULL, 
                   CREATE_ALWAYS, 
                   FILE_ATTRIBUTE_NORMAL, 
                   (HANDLE) NULL); 
  if(hf==INVALID_HANDLE_VALUE){
  MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);      
  }
 hdr.bfType=0x4d42;    
 hdr.bfSize=(DWORD)(sizeof(BITMAPFILEHEADER)+pbih->biSize + pbih->biClrUsed*sizeof(RGBQUAD)+pbih->biSizeImage); 
 hdr.bfReserved1=0; 
 hdr.bfReserved2=0; 
 hdr.bfOffBits=(DWORD)sizeof(BITMAPFILEHEADER)+pbih->biSize + pbih->biClrUsed*sizeof (RGBQUAD); 
  if(!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL)){
  MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);               
  }
  if(!WriteFile(hf,(LPVOID)pbih,sizeof(BITMAPINFOHEADER)+pbih->biClrUsed * sizeof (RGBQUAD),(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL)){
  MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);          
  }
 dwTotal = cb = pbih->biSizeImage; 
 hp = lpBits; 
  while(cb > MAXWRITE){ 
   if(!WriteFile(hf,(LPSTR)hp,(int)MAXWRITE,(LPDWORD)&dwTmp,(LPOVERLAPPED)NULL)){
   MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);                                                                              
   } 
  cb-=MAXWRITE; 
  hp+=MAXWRITE; 
  } 
  if(!WriteFile(hf, (LPSTR) hp, (int) cb,(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL)){
  MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);                  
  } 
  if(!CloseHandle(hf)){
  MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);                     
  } 
 GlobalFree((HGLOBAL)lpBits);
 } 
 
 HBITMAP hbitmap;//The bitmap

 public:   
 
 //Constructor
 Bitmap(){};
 

 //Constructor specifying either a resource name or a bitmap file path\name.txt
 Bitmap(char* FOR){
 this->hbitmap=LoadBitmap(0,FOR);
  if(!this->hbitmap){
  this->hbitmap=(HBITMAP)LoadImage(NULL,FOR,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);            
  }
 }
 
 //If the bitmap contains data or not
 BOOL Exists(){
  if(!this->hbitmap){
  return FALSE;     
  }     
 return TRUE;     
 } 

 //Skipping through the obvious ones now...
 HBITMAP GetHandle(){
 return this->hbitmap;            
 }
 
 HBITMAP SetHandle(HBITMAP hbm,HWND hwnd=(HWND)NULL,RECT* r=(RECT*)NULL){
 HBITMAP temp;
 temp=this->hbitmap;
 this->hbitmap=hbm;
  if(hwnd){   
  RECT rect;
   if(!r){
   GetClientRect(hwnd,&rect);
   }
   else{
   rect=*r;     
   }
  UpdateWindow(hwnd);
  RedrawWindow(hwnd,&rect,0,RDW_ERASE|RDW_INVALIDATE);
  }
 return temp;
 }
 
 HBITMAP SetHandle(char*FOR,HWND hwnd=(HWND)NULL,RECT* r=(RECT*)NULL){
 HBITMAP temp=this->hbitmap;
 this->hbitmap=LoadBitmap(GetModuleHandle(0),FOR);
  if(!this->hbitmap){
  this->hbitmap=(HBITMAP)LoadImage(NULL,FOR,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);            
  }
  if(hwnd){   
  RECT rect;
   if(!r){
   GetClientRect(hwnd,&rect);
   }
   else{
   rect=*r;     
   }
  UpdateWindow(hwnd);
  RedrawWindow(hwnd,&rect,0,RDW_ERASE|RDW_INVALIDATE);
  }
 return temp;
 }
 //This one nullifies the bitmap
 void ClearHandle(HWND hwnd=(HWND)NULL,RECT* r=(RECT*)NULL){
 this->hbitmap=(HBITMAP)NULL;
  if(hwnd){   
  RECT rect;
   if(!r){
   GetClientRect(hwnd,&rect);
   }
   else{
   rect=*r;     
   }
  UpdateWindow(hwnd);
  RedrawWindow(hwnd,&rect,0,RDW_ERASE|RDW_INVALIDATE);
  }
 }
 
 //Draw the bitmap to a DC, arguments are obvious, bitmap is sized 
 //automatically according to it's actual size.
 void Draw(HDC hdc,DWORD mode=SRCCOPY,int x=0,int y=0){
 BITMAP bm;
 GetObject(this->hbitmap,sizeof(BITMAP),&bm);
 int w=bm.bmWidth;
 int h=bm.bmHeight;    
 HDC src=CreateCompatibleDC(hdc);
 SelectObject(src,this->hbitmap);
 BitBlt(hdc,x,y,w,h,src,0,0,mode);
 DeleteDC(src);         
 }

 
 void DrawTransparent(HDC hdc,COLORREF transparentColor=RGB(255,255,255),int x=0,int y=0){
 DrawTransparentBitmap(hdc,this->hbitmap,x,y,transparentColor);
 }
 
 //Save to the specified file
 void SaveToFile(char* file){
 BITMAPINFO* bmi=CreateBitmapInfoStruct(this->hbitmap);
 CreateBMPFile(file,bmi,this->hbitmap,GetDC(GetDesktopWindow()));
 }
};
[/code]
r_heidari 0 Newbie Poster

Thanks a lot , it works fine!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.