Bitmap class

Please support our C++ advertiser: Intel Parallel Studio Home
wavsyntax wavsyntax is offline Offline Jun 3rd, 2009, 6:50 am |
0
Enjoy.
Quick reply to this message  
C++ Syntax
  1. [/code]
  2. #define MAXWRITE (1024*16)
  3.  
  4. class Bitmap{
  5. private:
  6. //This function is slow, and thus not good for animation purposes
  7. //but for displaying a transparent bitmap that doesn't get redrawn
  8. //constantly this is a good function
  9. void DrawTransparentBitmap(HDC hdc,HBITMAP hbm,int ix=0,int iy=0,COLORREF transClr=RGB(255,255,255)){
  10. BITMAP bm;
  11. int tempx=ix,tempy=iy;
  12. GetObject(hbm,sizeof(bm),&bm);
  13. HDC hdc2=CreateCompatibleDC(hdc);
  14. SelectObject(hdc2,hbm);
  15. int xx=0,yy=0;
  16. for(int y=tempy;y<tempy+bm.bmHeight;y++){
  17. for(int x=tempx;x<tempx+bm.bmWidth;x++){
  18. COLORREF clr=GetPixel(hdc2,xx,yy);
  19. if(clr!=transClr){
  20. SetPixel(hdc,x,y,clr);
  21. }
  22. xx++;
  23. }
  24. xx=0;
  25. yy++;
  26. }
  27. DeleteDC(hdc2);
  28. }
  29.  
  30.  
  31. //I added this function from win32 api.hlp, I plan to write my own some day
  32. PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp) {
  33. BITMAP bmp;
  34. PBITMAPINFO pbmi;
  35. WORD cClrBits;
  36. if(!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp)){
  37. MessageBox(0,"Bitmap failed to save","Error...",MB_OK);
  38. }
  39. cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
  40. if(cClrBits == 1){
  41. cClrBits = 1;
  42. }
  43. else if(cClrBits <= 4){
  44. cClrBits = 4;
  45. }
  46. else if(cClrBits <= 8){
  47. cClrBits = 8;
  48. }
  49. else if(cClrBits <= 16){
  50. cClrBits = 16;
  51. }
  52. else if(cClrBits <= 24){
  53. cClrBits = 24;
  54. }
  55. else{
  56. cClrBits = 32;
  57. }
  58. if(cClrBits != 24){
  59. pbmi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD) * (2^cClrBits));
  60. }
  61. else{
  62. pbmi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER));
  63. }
  64. pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  65. pbmi->bmiHeader.biWidth = bmp.bmWidth;
  66. pbmi->bmiHeader.biHeight = bmp.bmHeight;
  67. pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
  68. pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
  69. if(cClrBits < 24){
  70. pbmi->bmiHeader.biClrUsed = 2^cClrBits;
  71. }
  72. pbmi->bmiHeader.biCompression = BI_RGB;
  73. pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8
  74. * pbmi->bmiHeader.biHeight
  75. * cClrBits;
  76. pbmi->bmiHeader.biClrImportant = 0;
  77. return pbmi;
  78. }
  79.  
  80.  
  81. //I added this function from win32 api.hlp, I plan to write my own some day
  82. void CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP,HDC hDC){
  83. HANDLE hf;
  84. BITMAPFILEHEADER hdr;
  85. PBITMAPINFOHEADER pbih;
  86. LPBYTE lpBits;
  87. DWORD dwTotal;
  88. DWORD cb;
  89. BYTE *hp;
  90. DWORD dwTmp;
  91. pbih = (PBITMAPINFOHEADER) pbi;
  92. lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
  93. if(!lpBits){
  94. MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);
  95. }
  96. if(!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS)){
  97. MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);
  98. }
  99. hf=CreateFile(pszFile,
  100. GENERIC_READ | GENERIC_WRITE,
  101. (DWORD) 0,
  102. (LPSECURITY_ATTRIBUTES) NULL,
  103. CREATE_ALWAYS,
  104. FILE_ATTRIBUTE_NORMAL,
  105. (HANDLE) NULL);
  106. if(hf==INVALID_HANDLE_VALUE){
  107. MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);
  108. }
  109. hdr.bfType=0x4d42;
  110. hdr.bfSize=(DWORD)(sizeof(BITMAPFILEHEADER)+pbih->biSize + pbih->biClrUsed*sizeof(RGBQUAD)+pbih->biSizeImage);
  111. hdr.bfReserved1=0;
  112. hdr.bfReserved2=0;
  113. hdr.bfOffBits=(DWORD)sizeof(BITMAPFILEHEADER)+pbih->biSize + pbih->biClrUsed*sizeof (RGBQUAD);
  114. if(!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL)){
  115. MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);
  116. }
  117. if(!WriteFile(hf,(LPVOID)pbih,sizeof(BITMAPINFOHEADER)+pbih->biClrUsed * sizeof (RGBQUAD),(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL)){
  118. MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);
  119. }
  120. dwTotal = cb = pbih->biSizeImage;
  121. hp = lpBits;
  122. while(cb > MAXWRITE){
  123. if(!WriteFile(hf,(LPSTR)hp,(int)MAXWRITE,(LPDWORD)&dwTmp,(LPOVERLAPPED)NULL)){
  124. MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);
  125. }
  126. cb-=MAXWRITE;
  127. hp+=MAXWRITE;
  128. }
  129. if(!WriteFile(hf, (LPSTR) hp, (int) cb,(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL)){
  130. MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);
  131. }
  132. if(!CloseHandle(hf)){
  133. MessageBox(0,"Failed to save bitmap.","Error...",MB_OK);
  134. }
  135. GlobalFree((HGLOBAL)lpBits);
  136. }
  137.  
  138. HBITMAP hbitmap;//The bitmap
  139.  
  140. public:
  141.  
  142. //Constructor
  143. Bitmap(){};
  144.  
  145.  
  146. //Constructor specifying either a resource name or a bitmap file path\name.txt
  147. Bitmap(char* FOR){
  148. this->hbitmap=LoadBitmap(0,FOR);
  149. if(!this->hbitmap){
  150. this->hbitmap=(HBITMAP)LoadImage(NULL,FOR,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
  151. }
  152. }
  153.  
  154. //If the bitmap contains data or not
  155. BOOL Exists(){
  156. if(!this->hbitmap){
  157. return FALSE;
  158. }
  159. return TRUE;
  160. }
  161.  
  162. //Skipping through the obvious ones now...
  163. HBITMAP GetHandle(){
  164. return this->hbitmap;
  165. }
  166.  
  167. HBITMAP SetHandle(HBITMAP hbm,HWND hwnd=(HWND)NULL,RECT* r=(RECT*)NULL){
  168. HBITMAP temp;
  169. temp=this->hbitmap;
  170. this->hbitmap=hbm;
  171. if(hwnd){
  172. RECT rect;
  173. if(!r){
  174. GetClientRect(hwnd,&rect);
  175. }
  176. else{
  177. rect=*r;
  178. }
  179. UpdateWindow(hwnd);
  180. RedrawWindow(hwnd,&rect,0,RDW_ERASE|RDW_INVALIDATE);
  181. }
  182. return temp;
  183. }
  184.  
  185. HBITMAP SetHandle(char*FOR,HWND hwnd=(HWND)NULL,RECT* r=(RECT*)NULL){
  186. HBITMAP temp=this->hbitmap;
  187. this->hbitmap=LoadBitmap(GetModuleHandle(0),FOR);
  188. if(!this->hbitmap){
  189. this->hbitmap=(HBITMAP)LoadImage(NULL,FOR,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
  190. }
  191. if(hwnd){
  192. RECT rect;
  193. if(!r){
  194. GetClientRect(hwnd,&rect);
  195. }
  196. else{
  197. rect=*r;
  198. }
  199. UpdateWindow(hwnd);
  200. RedrawWindow(hwnd,&rect,0,RDW_ERASE|RDW_INVALIDATE);
  201. }
  202. return temp;
  203. }
  204. //This one nullifies the bitmap
  205. void ClearHandle(HWND hwnd=(HWND)NULL,RECT* r=(RECT*)NULL){
  206. this->hbitmap=(HBITMAP)NULL;
  207. if(hwnd){
  208. RECT rect;
  209. if(!r){
  210. GetClientRect(hwnd,&rect);
  211. }
  212. else{
  213. rect=*r;
  214. }
  215. UpdateWindow(hwnd);
  216. RedrawWindow(hwnd,&rect,0,RDW_ERASE|RDW_INVALIDATE);
  217. }
  218. }
  219.  
  220. //Draw the bitmap to a DC, arguments are obvious, bitmap is sized
  221. //automatically according to it's actual size.
  222. void Draw(HDC hdc,DWORD mode=SRCCOPY,int x=0,int y=0){
  223. BITMAP bm;
  224. GetObject(this->hbitmap,sizeof(BITMAP),&bm);
  225. int w=bm.bmWidth;
  226. int h=bm.bmHeight;
  227. HDC src=CreateCompatibleDC(hdc);
  228. SelectObject(src,this->hbitmap);
  229. BitBlt(hdc,x,y,w,h,src,0,0,mode);
  230. DeleteDC(src);
  231. }
  232.  
  233.  
  234. void DrawTransparent(HDC hdc,COLORREF transparentColor=RGB(255,255,255),int x=0,int y=0){
  235. DrawTransparentBitmap(hdc,this->hbitmap,x,y,transparentColor);
  236. }
  237.  
  238. //Save to the specified file
  239. void SaveToFile(char* file){
  240. BITMAPINFO* bmi=CreateBitmapInfoStruct(this->hbitmap);
  241. CreateBMPFile(file,bmi,this->hbitmap,GetDC(GetDesktopWindow()));
  242. }
  243. };
  244. [/code]

Message:


Similar Threads
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC