一幅图片,用char* buffer存储,三个字符RGB为一个像素,这样的存法,有没人弄过?我想找个方法把它显示出来,验证我写的算法是否正确限制要求是win32,非MFC有代码提供是最好了没有的话,搜索的关键字也行的。。

解决方案 »

  1.   

    pBits就是你说的char* buffer代码是MFC图像保存改编过来的。没测试,这是流程。
    //图形格式参数
    LPBITMAPINFO lpbmih = new BITMAPINFO;
    lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    lpbmih->bmiHeader.biWidth = iWidth;
    lpbmih->bmiHeader.biHeight = iHeight;
    lpbmih->bmiHeader.biPlanes = 1;
    lpbmih->bmiHeader.biBitCount = iPixel;
    lpbmih->bmiHeader.biCompression = BI_RGB;
    lpbmih->bmiHeader.biSizeImage = 0;
    lpbmih->bmiHeader.biXPelsPerMeter = 0;
    lpbmih->bmiHeader.biYPelsPerMeter = 0;
    lpbmih->bmiHeader.biClrUsed = 0;
    lpbmih->bmiHeader.biClrImportant = 0;


    //保存到文件并创建位图结构
    BITMAPFILEHEADER bmfh;
    ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER));
    *((char *)&bmfh.bfType) = 'B';
    *(((char *)&bmfh.bfType) + 1) = 'M';
    bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bmfh.bfSize = bmfh.bfOffBits + (iWidth * iHeight) * iPixel / 8;
    TCHAR szBMPFileName[128];
    int iBMPBytes = iWidth * iHeight * iPixel / 8;

    file.Write(&bmfh,sizeof(BITMAPFILEHEADER));
    file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER));
    file.Write(pBits,iBMPBytes);
    file.Close();
      

  2.   

    StretchDIBits
    The StretchDIBits function copies the color data for a rectangle of pixels in a device-independent bitmap (DIB) to the specified destination rectangle. If the destination rectangle is larger than the source rectangle, this function stretches the rows and columns of color data to fit the destination rectangle. If the destination rectangle is smaller than the source rectangle, this function compresses the rows and columns by using the specified raster operation.  pDC->SetStretchBltMode(COLORONCOLOR);
    StretchDIBits(pDC->GetSafeHdc(),xStart,yStart,width,height,
    0,0,width,height,
    bitsAt,
    //&BmpInfo,//without RGBQUAD
    (BITMAPINFO *)pBMPinfo,// has RGBQUAD
    DIB_RGB_COLORS,SRCCOPY);
      

  3.   


    file是fstream?类型写入写不进去。。
      

  4.   


    我就是从画布上拿下来的buffer但画布往哪去打印,这个不会。。
      

  5.   

    你那个(这样的存法)已经存为文件了吧,
    显示时读回数据,即bits。
    按你知道的信息(高宽)填一个:BMPinfo
    再调用StretchDIBits
    就可以了。
      

  6.   

    上面几个方法都没用成,win的api太复杂不确定哪个才是正确的