• 一些基本的GDI操作BITMAP的方法


    转自:http://www.cppblog.com/richardzeng/archive/2006/03/10/3962.html

     1 
     2 #ifndef _BITMAP_H
     3 #define _BITMAP_H
     4 
     5 #include <windows.h>
     6 
     7 void SaveImage(const char * bmp_file,void *rgb_data,int rgb_len,BITMAPINFOHEADER * bi);
     8 void FillBitmapInfoHdr(BITMAPINFOHEADER * bi,int width,int height,int deep);
     9 bool GetImageData( HWND target,RECT rect,void ** data,int * len);
    10 bool GetImageData2( HWND target,RECT rect,void * data,int * len);
    11 void CaptureImage(HWND target,const char * destfile);
    12 void DrawBitmap(HWND hwnd,int width,int height,char *data,int deep);            
    13 void DrawBitmap2(HWND hwnd,RECT rect,char *data,int deep);
    14 void StretchDrawBitmap(HWND hwnd,int width,int height,char *data,int deep);
    15 #endif
    16 
      1 #include <windows.h>
      2 #include <stdio.h>
      3 
      4 void SaveImage(const char * bmp_file,void *rgb_data,int rgb_len,BITMAPINFOHEADER * bi){
      5     BITMAPFILEHEADER hdr;
      6     FILE *fsave;
      7     memset(&hdr,0,sizeof(hdr));
      8     hdr.bfType = ((WORD) ('M' << 8) | 'B');
      9     hdr.bfSize = sizeof(hdr)+sizeof(BITMAPINFOHEADER)+rgb_len;
     10     hdr.bfOffBits = (DWORD) (sizeof(hdr) + sizeof(BITMAPINFOHEADER) );
     11     //--
     12     fsave = fopen(bmp_file,"wb");
     13     fwrite( &hdr,sizeof(hdr),1,fsave);
     14     fwrite(bi,sizeof(BITMAPINFOHEADER),1,fsave);
     15     fwrite(rgb_data,rgb_len,1,fsave);
     16     fflush(fsave);
     17     fclose(fsave);
     18 }
     19 
     20 
     21 void FillBitmapInfoHdr(BITMAPINFOHEADER * bi,int width,int height,int deep){
     22     memset(bi,0,sizeof(BITMAPINFOHEADER));
     23     bi->biSize        = sizeof(BITMAPINFOHEADER);
     24     bi->biWidth        = width;
     25     bi->biHeight         = height;
     26     bi->biPlanes         = 1;
     27     bi->biBitCount        = deep;            //bm.bmPlanes * bm.bmBitsPixel;
     28     bi->biCompression    = BI_RGB;
     29     bi->biSizeImage        = 0;
     30     bi->biXPelsPerMeter    = 0;
     31     bi->biYPelsPerMeter    = 0;
     32     bi->biClrUsed        = 0;
     33     bi->biClrImportant    = 0;
     34 }
     35 
     36 bool GetImageData2( HWND target,RECT rect,void * data,int * len){
     37      HDC entireDC;
     38     RECT rc;
     39     HBITMAP bitmap;
     40     HDC CompDC;    
     41     entireDC = GetDC(target);
     42     //GetWindowRect(target,&rc);
     43     rc = rect;   
     44     
     45     if( rc.right-rc.left<=0 || rc.bottom-rc.top <=0){
     46         return false;
     47     }
     48     bitmap = CreateCompatibleBitmap( entireDC,rc.right-rc.left,rc.bottom-rc.top);
     49     if(bitmap ==NULL){
     50         ReleaseDC(target,entireDC);
     51         return false;
     52     }
     53     CompDC = CreateCompatibleDC(entireDC);
     54     SelectObject(CompDC,bitmap);    
     55     BitBlt(CompDC,0,0,rc.right-rc.left,rc.bottom-rc.top,entireDC,rect.left,rect.top,SRCCOPY);
     56 
     57     BITMAPINFO bmi;
     58       BITMAPINFOHEADER   *bi;
     59       int rgb_len ;
     60     int lines;
     61     bi = &bmi.bmiHeader;
     62       char *rgb_buff ;
     63     FillBitmapInfoHdr(bi,rc.right-rc.left,rc.bottom-rc.top,24);
     64 
     65     rgb_len = bi->biWidth * bi->biHeight * 3;
     66     
     67     rgb_buff =(char*) data;// new char[rgb_len];
     68     if( rgb_buff == NULL ){
     69         return false;
     70     }
     71     lines = GetDIBits( CompDC,bitmap,0,rc.bottom-rc.top,rgb_buff,
     72                         (LPBITMAPINFO)bi,DIB_RGB_COLORS);
     73     DeleteObject(bitmap);
     74     ReleaseDC(target,CompDC);
     75     ReleaseDC(target,entireDC);
     76     DeleteDC(CompDC);
     77     if( lines == NULL){
     78       //  delete[] rgb_buff;
     79         return false;
     80     }
     81    // *data = rgb_buff;
     82     *len = rgb_len;
     83     return true;
     84 }
     85 
     86 
     87 bool GetImageData( HWND target,RECT rect,void ** data,int * len){
     88      HDC entireDC;
     89     RECT rc;
     90     HBITMAP bitmap;
     91     HDC CompDC;    
     92     entireDC = GetDC(target);
     93     //GetWindowRect(target,&rc);
     94     rc = rect;    
     95     bitmap = CreateCompatibleBitmap( entireDC,rc.right-rc.left,rc.bottom-rc.top);
     96     CompDC = CreateCompatibleDC(entireDC);
     97     SelectObject(CompDC,bitmap);    
     98     BitBlt(CompDC,0,0,rc.right-rc.left,rc.bottom-rc.top,entireDC,0,0,SRCCOPY);
     99 
    100     BITMAPINFO bmi;
    101       BITMAPINFOHEADER   *bi;
    102       int rgb_len ;
    103     int lines;
    104     bi = &bmi.bmiHeader;
    105       char *rgb_buff ;
    106     FillBitmapInfoHdr(bi,rc.right-rc.left,rc.bottom-rc.top,24);
    107 
    108     rgb_len = bi->biWidth * bi->biHeight * 3;
    109     rgb_buff = new char[rgb_len];
    110     if( rgb_buff == NULL ){
    111         return false;
    112     }
    113     lines = GetDIBits( CompDC,bitmap,0,rc.bottom-rc.top,rgb_buff,
    114                         (LPBITMAPINFO)bi,DIB_RGB_COLORS);
    115     DeleteObject(bitmap);
    116     ReleaseDC(target,CompDC);
    117     ReleaseDC(target,entireDC);
    118     DeleteDC(CompDC);
    119     if( lines == NULL){
    120         delete[] rgb_buff;
    121         return false;
    122     }
    123     *data = rgb_buff;
    124     *len = rgb_len;
    125     return true;
    126 }
    127 
    128 
    129 void DrawBitmap(HWND hwnd,int width,int height,char *data,int deep);
    130 void CaptureImage(HWND target,const char * destfile){
    131     RECT rc;
    132     GetWindowRect(target,&rc);
    133     void *data;
    134     int len;
    135     GetImageData(target,rc,&data,&len);
    136 
    137     BITMAPINFO bmi;
    138       BITMAPINFOHEADER   *bi;      
    139     bi = &bmi.bmiHeader;
    140     FillBitmapInfoHdr(bi,rc.right-rc.left,rc.bottom-rc.top,24);
    141 
    142     SaveImage(destfile,data,len,bi);
    143     
    144     delete[] data;
    145 }
    146 
    147 void DrawBitmap2(HWND hwnd,RECT rect,char *data,int deep){
    148 
    149     BITMAPINFOHEADER   *bi;
    150     BITMAPINFO bitmap;
    151     bi = &bitmap.bmiHeader;
    152     memset(&bitmap,0,sizeof(bitmap));
    153     bi->biSize        = sizeof(BITMAPINFOHEADER);
    154     bi->biWidth        = rect.right - rect.left;
    155     bi->biHeight         = rect.bottom - rect.top;
    156     bi->biPlanes         = 1;
    157     bi->biBitCount        = deep;            //bm.bmPlanes * bm.bmBitsPixel;
    158     bi->biCompression    = BI_RGB;
    159     bi->biSizeImage        = bi->biWidth*bi->biHeight*deep/3;
    160     bi->biXPelsPerMeter    = 0;
    161     bi->biYPelsPerMeter    = 0;
    162     bi->biClrUsed        = 0;
    163     bi->biClrImportant    = 0;
    164 
    165     HDC hdc =   GetDC(hwnd);
    166 
    167     StretchDIBits( hdc,
    168         rect.left,
    169         rect.top,
    170         rect.right-rect.left,
    171         rect.bottom - rect.top,
    172         //rc.right-1,
    173         //rc.bottom-1,
    174         0,
    175         0,
    176         rect.right-rect.left,
    177         rect.bottom - rect.top,
    178         (CONST VOID *)data,
    179         &bitmap,
    180         DIB_RGB_COLORS,
    181         SRCCOPY );
    182 
    183     //ReleaseDC(hwnd,hdc);
    184     ReleaseDC(hwnd,hdc);
    185 }
    186 
    187 void StretchDrawBitmap(HWND hwnd,int width,int height,char *data,int deep){
    188     
    189     BITMAPINFOHEADER   *bi;
    190     BITMAPINFO bitmap;
    191     bi = &bitmap.bmiHeader;
    192     memset(&bitmap,0,sizeof(bitmap));
    193     bi->biSize        = sizeof(BITMAPINFOHEADER);
    194     bi->biWidth        = width;
    195     bi->biHeight         = height;
    196     bi->biPlanes         = 1;
    197     bi->biBitCount        = deep;            //bm.bmPlanes * bm.bmBitsPixel;
    198     bi->biCompression    = BI_RGB;
    199     bi->biSizeImage        = bi->biWidth*bi->biHeight*deep/3;
    200     bi->biXPelsPerMeter    = 0;
    201     bi->biYPelsPerMeter    = 0;
    202     bi->biClrUsed        = 0;
    203     bi->biClrImportant    = 0;
    204     RECT rc;
    205     HDC hdc =   GetDC(hwnd);
    206     ::GetWindowRect(hwnd,&rc);
    207     StretchDIBits( hdc,
    208         0,
    209         0,
    210         rc.right - rc.left,
    211         rc.bottom - rc.top,
    212         0,
    213         0,
    214         width,
    215         height,
    216         (CONST VOID *)data,
    217         &bitmap,
    218         DIB_RGB_COLORS,
    219         SRCCOPY );
    220     
    221     //ReleaseDC(hwnd,hdc);
    222     ReleaseDC(hwnd,hdc);
    223 }
    224 
    225 void DrawBitmap(HWND hwnd,int width,int height,char *data,int deep){
    226 
    227     BITMAPINFOHEADER   *bi;
    228     BITMAPINFO bitmap;
    229     bi = &bitmap.bmiHeader;
    230     memset(&bitmap,0,sizeof(bitmap));
    231     bi->biSize        = sizeof(BITMAPINFOHEADER);
    232     bi->biWidth        = width;
    233     bi->biHeight         = height;
    234     bi->biPlanes         = 1;
    235     bi->biBitCount        = deep;            //bm.bmPlanes * bm.bmBitsPixel;
    236     bi->biCompression    = BI_RGB;
    237     bi->biSizeImage        = width*height*deep;
    238     bi->biXPelsPerMeter    = 0;
    239     bi->biYPelsPerMeter    = 0;
    240     bi->biClrUsed        = 0;
    241     bi->biClrImportant    = 0;
    242 
    243     HDC hdc =   GetDC(hwnd);
    244 
    245     StretchDIBits( hdc,
    246         0,
    247         0,
    248         width-1,
    249         height-1,
    250         //rc.right-1,
    251         //rc.bottom-1,
    252         0,
    253         0,
    254         width,
    255         height,
    256         (CONST VOID *)data,
    257         &bitmap,
    258         DIB_RGB_COLORS,
    259         SRCCOPY );
    260 
    261     //ReleaseDC(hwnd,hdc);
    262     ReleaseDC(hwnd,hdc);
    263 }
  • 相关阅读:
    背景图片自适应大小(平铺)
    墨卡托投影示意图
    C# 两个类的实例之间相同属性的值的复制
    C# 并行编程 Task
    C# 并行编程 PLINQ
    C# 并行编程 Parallel
    仰望星空
    Ubuntu的人道精神
    神经网络简介
    并行计算简介
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/2855189.html
Copyright © 2020-2023  润新知