我用CreateCompatibleBitmap函数创建了一个DDB位图,选入设备场景后,画图.但是我不知道该如何抱存图像了,我不知到如何获得位图信息头等存文件的必须信息,请帮忙!!最哦好能给点代码我看看.Win32编成的.谢谢!!

解决方案 »

  1.   

    给一段以前在DOS下显示BMP文件的代码,有BMP文件格式等,看看能不能用
    主要是几个struct 和 show_bitmap(int xx,int yy,int w,int h,char *filename)unsigned long RGB(unsigned char red,unsigned char green,unsigned char blue)
    {  return (((unsigned long)blue)|((unsigned long)green<<8)|((unsigned long)red<<16));
    }void PutPixelTrueColor(int x,int y,unsigned long color)
    {  unsigned char rgb[3];
       unsigned char far *videoptr;
       unsigned page;
       unsigned long pos;
       int i;
       rgb[0]=(unsigned char)(color>>16);
       rgb[1]=(unsigned char)(color>>8);
       rgb[2]=(unsigned char)(color);
       pos=y*1920L+x*3;
       for (i=0;i<3;i++)
       {  page=(unsigned)(pos>>16);
          if (curpage!=page) selectpage(page);
          page=(unsigned)pos;
          videoptr=(unsigned char far *)MK_FP(0xa000,page);
          *videoptr=rgb[i];
          pos++;
       }
    }void setnormalmode(int mode)
    {  asm {
         mov  ax,mode
         xor  ah,ah
         int  0x10
      }
    }typedef unsigned int  UINT;
    typedef unsigned long DWORD;typedef struct tagBITMAPFILEHEADER
    {   UINT  bfType;
        DWORD bfSize;
        UINT  bfReserved1;
        UINT  bfReserved2;
        DWORD bfOffBits;
    } BMPFILEHEADER;typedef struct tagBITMAPINFOHEADER
    {   DWORD   biSize;
        LONG    biWidth;
        LONG    biHeight;
        WORD    biPlanes;    //1
        WORD    biBitCount;  //4
        DWORD   biCompression;  //0
        DWORD   biSizeImage;
        LONG    biXPelsPerMeter;
        LONG    biYPelsPerMeter;
        DWORD   biClrUsed;
        DWORD   biClrImportant;
    } BITMAPINFOHEADER;typedef struct
    { unsigned char red;
      unsigned char green;
      unsigned char blue;
      unsigned char reserved;
    } RGB_COLOR;RGB_COLOR rgbcol[256];
    XMS  xmsimg;unsigned long rgbcol16[16]=
    {0x000000,0x7f0000,0x007f00,0x7f7f00,0x00007f,0x7f007f,0x007f7f,0xc5c5c5,
     0x7f7f7f,0xff0000,0x00ff00,0xffff00,0x0000ff,0xff00ff,0x00ffff,0xffffff
    };unsigned char buffer[4096];RGB_COLOR appcol[16];void SaveAppColor(void)
    {  int i;
       for (i=0;i<16;i++)
       {  outportb(0x3c7,i);
          appcol[i].blue=((inportb(0x3c9))<<2);
          appcol[i].green=((inportb(0x3c9))<<2);
          appcol[i].red=((inportb(0x3c9))<<2);
    //      appcol[i].reserved=inportb(0x3c9);
       }
    }
    void img16totruecolor(void)
    {  unsigned char *plane[4]={buffer,buffer+80,buffer+160,buffer+240},c,cc;
       int i,k,l;
       long pos[4];
       for (k=0;k<4;k++) pos[k]=k*38400L+1024L;
       for (i=0;i<480;i++)
       {  for (k=0;k<4;k++)
          {  xmsimg.Read(pos[k],plane[k],80);
     pos[k]+=80;
          }
          for (k=0;k<80;k++)
          {  for (l=0,c=0x80;l<8;l++,c>>=1)
     {  cc=0;
        if (plane[0][k]&c) cc|=1;
        if (plane[1][k]&c) cc|=2;
        if (plane[2][k]&c) cc|=4;
        if (plane[3][k]&c) cc|=8;
        PutPixelTrueColor(k*8+l,i,RGB(appcol[cc].red,appcol[cc].green,appcol[cc].blue));
    //          rgbcol16[cc]);
     }
          }
       }
    }int show_bitmap(int xx,int yy,int w,int h,char *filename)
    {  int handle,x,y,linelen;
       BMPFILEHEADER header;
       BITMAPINFOHEADER bmph;
       unsigned char *p;
       handle=_open(filename,O_BINARY|O_RDONLY|O_DENYNONE);
       if (handle==-1) return -1;    //file open error
       _read(handle,&header,sizeof(header));
       if (header.bfType!=0x4d42)  { _close(handle); return -2;}  //not bitmap file
       _read(handle,&bmph,sizeof(bmph));
       if ((bmph.biWidth>w)||(bmph.biHeight>h))
       { _close(handle);
         return -4;
       }
       xx+=(w-bmph.biWidth)/2;
       yy+=(h-bmph.biHeight)/2;
       if ((bmph.biPlanes!=1)||(bmph.biBitCount!=24)||(bmph.biCompression!=0))
       {  if (bmph.biBitCount!=8)
          { _close(handle);
    return -3;
          }
          _read(handle,&rgbcol,256*sizeof(RGB_COLOR));
          lseek(handle,header.bfOffBits,0);
          linelen=bmph.biWidth;
          for (y=bmph.biHeight-1;y>=0;y--)
          {  p=buffer;
    _read(handle,p,linelen);
    for (x=0;x<bmph.biWidth;x++,p++)
       PutPixelTrueColor(x+xx,y+yy,RGB(rgbcol[*p].red,rgbcol[*p].green,rgbcol[*p].blue));
          }
          _close(handle);
          return 1;
       }  //not 640x480 24bits colors no compressed bitmap file
       lseek(handle,header.bfOffBits,0);
       linelen=bmph.biSizeImage/bmph.biHeight;
       if (linelen>bmph.biWidth*3+8) linelen=bmph.biWidth*3;
    //   linelen=(bmph.biWidth)*3;
       for (y=bmph.biHeight-1;y>=0;y--)
       {  p=buffer;
          _read(handle,p,linelen);
          for (x=0;x<bmph.biWidth;x++,p+=3) PutPixelTrueColor(x+xx,y+yy,RGB(p[0],p[1],p[2]));
       }
       _close(handle);
       return 1;
    }
      

  2.   

    http://www.csdn.net/expert/topic/823/823129.xml?temp=.8448145
    这个主题中有个人贴出了这样的代码,其中有个函数是DIB TO File。你参考一下。
      

  3.   

    楼上,你所说的东西哪里有啊?
    [email protected]
      

  4.   

    codeproject上有教程和例子:
    Windows GDI Tutorial 4 - DIBs
    http://www.codeproject.com/bitmap/gditutorial.asp
      

  5.   

    这里还有两个函数,可以直接用,DDBToDIB()和WriteDIB():HANDLE DDBToDIB(HBITMAP hBitmap, DWORD dwCompression, CPalette *pPal)
    {
    BITMAP bm;
    BITMAPINFOHEADER bi;
    LPBITMAPINFOHEADER  lpbi;
    DWORD dwLen;
    HANDLE hDIB;
    HANDLE handle;
    HDC  hDC;
    HPALETTE hPal;
    // The function has no arg for bitfields
    if( dwCompression == BI_BITFIELDS )
    return NULL; // If a palette has not been supplied use defaul palette
    hPal = (HPALETTE) pPal->GetSafeHandle();
    if (hPal==NULL)
    hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); // Get bitmap information
    GetObject(hBitmap, sizeof(bm),(LPSTR)&bm); // Initialize the bitmapinfoheader
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bm.bmWidth;
    bi.biHeight  = bm.bmHeight;
    bi.biPlanes  = 1;
    bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
    bi.biCompression = dwCompression;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0; // Compute the size of the  infoheader and the color table
    int nColors = (1 << bi.biBitCount);
    if( nColors > 256 ) 
    nColors = 0;
    dwLen  = bi.biSize + nColors * sizeof(RGBQUAD); // We need a device context to get the DIB from
    hDC = ::GetDC(NULL);
    hPal = SelectPalette(hDC,hPal,FALSE);
    RealizePalette(hDC); // Allocate enough memory to hold bitmapinfoheader and color table
    hDIB = GlobalAlloc(GMEM_FIXED,dwLen); if (!hDIB){
    SelectPalette(hDC,hPal,FALSE);
    ::ReleaseDC(NULL,hDC);
    return NULL;
    } lpbi = (LPBITMAPINFOHEADER)hDIB; *lpbi = bi; // Call GetDIBits with a NULL lpBits param, so the device driver 
    // will calculate the biSizeImage field 
    GetDIBits(hDC, hBitmap, 0L, (DWORD)bi.biHeight,
    (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS); bi = *lpbi; // If the driver did not fill in the biSizeImage field, then compute it
    // Each scan line of the image is aligned on a DWORD (32bit) boundary
    if (bi.biSizeImage == 0){
    bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8) 
    * bi.biHeight; // If a compression scheme is used the result may infact be larger
    // Increase the size to account for this.
    if (dwCompression != BI_RGB)
    bi.biSizeImage = (bi.biSizeImage * 3) / 2;
    } // Realloc the buffer so that it can hold all the bits
    dwLen += bi.biSizeImage;
    if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
    hDIB = handle;
    else{
    GlobalFree(hDIB); // Reselect the original palette
    SelectPalette(hDC,hPal,FALSE);
    ::ReleaseDC(NULL,hDC);
    return NULL;
    } // Get the bitmap bits
    lpbi = (LPBITMAPINFOHEADER)hDIB; // FINALLY get the DIB
    BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
    0L, // Start scan line
    (DWORD)bi.biHeight, // # of scan lines
    (LPBYTE)lpbi  // address for bitmap bits
    + (bi.biSize + nColors * sizeof(RGBQUAD)),
    (LPBITMAPINFO)lpbi, // address of bitmapinfo
    (DWORD)DIB_RGB_COLORS); // Use RGB for color table if( !bGotBits )
    {
    GlobalFree(hDIB);

    SelectPalette(hDC,hPal,FALSE);
    ::ReleaseDC(NULL,hDC);
    return NULL;
    } SelectPalette(hDC,hPal,FALSE);
    ::ReleaseDC(NULL,hDC);
    return hDIB;}BOOL WriteDIB(LPTSTR szFile, HANDLE hDIB)
    {
    BITMAPFILEHEADER hdr;
    LPBITMAPINFOHEADER lpbi; if (!hDIB)
    return FALSE; CFile file;
    if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
    return FALSE; lpbi = (LPBITMAPINFOHEADER)hDIB; int nColors = 1 << lpbi->biBitCount; // Fill in the fields of the file header 
    hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
    hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
    hdr.bfReserved1  = 0;
    hdr.bfReserved2  = 0;
    hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
    nColors * sizeof(RGBQUAD)); // Write the file header 
    file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits 
    file.Write( lpbi, GlobalSize(hDIB) ); return TRUE;}
      

  6.   

    谢谢楼上的伙计。但是我主要是CreateCompatibleBitmap函数并没有给出相应的位图信息,所以我在存盘时就不知如何写文件头信息头等了,大家再帮忙!
      

  7.   

    以下就是BMP文件的头啊typedef struct tagBITMAPFILEHEADER
    {   UINT  bfType;
        DWORD bfSize;
        UINT  bfReserved1;
        UINT  bfReserved2;
        DWORD bfOffBits;
    } BMPFILEHEADER;typedef struct tagBITMAPINFOHEADER
    {   DWORD   biSize;
        LONG    biWidth;
        LONG    biHeight;
        WORD    biPlanes;    //1
        WORD    biBitCount;  //4
        DWORD   biCompression;  //0
        DWORD   biSizeImage;
        LONG    biXPelsPerMeter;
        LONG    biYPelsPerMeter;
        DWORD   biClrUsed;
        DWORD   biClrImportant;
    } BITMAPINFOHEADER;typedef struct
    { unsigned char red;
      unsigned char green;
      unsigned char blue;
      unsigned char reserved;
    } RGB_COLOR;
      

  8.   

    建议看《图像处理C原玛分析》
    你一定会有收获e.pku.edu.cn  选择FTP
    搜索还有就是 《Visual C++ 图像处理》
    书店头有