C Win32: save .bmp image from HBITMAP

前端 未结 4 2119
谎友^
谎友^ 2021-02-20 07:13

I am working with a framegrabber and need to get the images from computer memory and save them on an image file. after trying for couple of days I end up with the following 2 fu

相关标签:
4条回答
  • 2021-02-20 07:47
    FORMAT_INFO sFormat;
    mvfg_getparam( MVFGPAR_DATAFORMAT, &sFormat, giCurrentGrabberID);
    long lFrameSize     = sFormat.lFrameSize;
    
    BYTE *pBitmap = (BYTE*)malloc(FrameSize);
    memcpy( pBitmap, CamGetPicture(), FrameSize );
    writeBitmapToFile(pBitmap, FrameSize, "tmp.bmp");
    
    /*
     * write our raw data into a bitmap file
     * adding the correct header and put it all
     * together in a single file
     */
    int
    writeBitmapToFile(void *data, unsigned long size, const char *filename)
    {
        bmpFileHeader_t header;
        bmpInfoHeader_t info;
        bmpRGBQuad_t rgb = { 0, 0, 0, 0};
        FILE *out;
        size_t len;
        int i;
    
    
        header.type = 0x4d42; // magic sequence 'BM'
        header.size = 0;
        header.res0 = 0;
        header.res1 = 0;
        /* 256 different colors, each is defined by an bmpRBQuad type */
        header.offset = 256 * sizeof(bmpRGBQuad_t) + 
                        sizeof(bmpInfoHeader_t) + sizeof(bmpFileHeader_t);
        info.size = sizeof(bmpInfoHeader_t);
        info.width = WIDTH;
        info.height = HEIGHT;
        info.planes = 1;
        info.cDepth = 8; /* 8 Bit */
        info.compression = 0;
        info.rawSize = size;
        info.hRes = 0;
        info.vRes = 0;
        info.nrColors = 0x100;
        info.impColors = 0;
    
        out = fopen(filename, "wb");
        if (out == NULL) {
            printf("error cannot open %s for writing\n", filename);
            return -1;
        }
        len = fwrite(&header, 1, sizeof(header), out);
        if (len != sizeof(header)) {
            printf("error while writing header\n");
            return -1;
        }
        len = fwrite(&info, 1, sizeof(info), out);
        if (len != sizeof(info)) {
            printf("error while writing info header\n");
            return -1;
        }
        /* stupid try and error programming leads to this */
        for (i = 0; i < 256; i++) {
            rgb.red = i;
            rgb.green = i;
            rgb.blue = i;
            len = fwrite(&rgb, 1, sizeof(rgb), out);
            if (len != sizeof(rgb)) {
                printf("error while writing rgb header\n");
                return -1;
            }
        }
    
        len = fwrite(data, 1, size, out);
        if (len != size ) {
            printf("error while writing bitmap data\n");
            return -1;
        }
        return 0;
    }
    

    Reference - mikrotron

    Reference - write bitmap

    0 讨论(0)
  • 2021-02-20 08:02

    I just find out I had a silly mistake in another part of my code I was using :

    // Convert Image to bitmap and display it
    DrawPicture( ghDCMain, pWinGBitmap, gpbImageData, lXSize, lYSize  );    
    if(counter!=1) {
      hBitmap2 = CreateCompatibleBitmap (hDCBits, lXSize, lYSize);
      SaveToFile(hBitmap2, "c:\\t.bmp");
      OutputDebugString("tested !!!!");
    }
    

    by deleting the hBitmap2 = CreateCompatibleBitmap (hDCBits, lXSize, lYSize); line, and changing the hBitmap2 to hBitmap the result of CreateDIBSection(), it saved the image beautifully. THANK YOU EVERYONE FOR YOUR HELP.

    0 讨论(0)
  • 2021-02-20 08:02

    From the documentation for CreateCompatibleDC:

    When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high. Before an application can use a memory DC for drawing operations, it must select a bitmap of the correct width and height into the DC.

    Since you're using CreateDIBSection with that DC, the resulting DIB section will be 1-bit monochrome as well.

    0 讨论(0)
  • 2021-02-20 08:03

    Are you sure there is proper data in there?

    I would try extract the RGB data and save it in some very simple format "manually", just to see what is in there. If you are looking for a dead simple but still standard image format, look at PPM images.

    0 讨论(0)
提交回复
热议问题