in CefRenderHandler->OnPaint can save the buffer to a bitmap

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

in CefRenderHandler->OnPaint can save the buffer to a bitmap

Postby yanyuejie » Thu Oct 31, 2013 2:07 am

in CefRenderHandler->OnPaint, how can I save the buffer to a bitmap? on windows, use the Bitmap.Bitmap(INT, INT, INT, PixelFormat, BYTE*) constructor, always get the wrong bitmap.
then i fount in the OnPaint api description "On Windows |buffer| will be |width|*|height|*4 bytes in size and represents a BGRA image with an upper-left origin", but in the Bitmap constructor the PixelFormat param just get PixelFormat32bppARGB,PixelFormat32bppPARGB for choose. so i use the htonl function to exchange the byte order, I get another bitmap but still not the right one.

Is there anyone get the bitmap directly from the render buffer successfully?how?

thanks.
yanyuejie
Techie
 
Posts: 29
Joined: Fri Jul 26, 2013 4:43 am

Re: in CefRenderHandler->OnPaint can save the buffer to a bi

Postby yanyuejie » Thu Oct 31, 2013 2:55 am

use the Bitmap->SetPixel() can get the right bitmap, but the efficiency is so poor.
yanyuejie
Techie
 
Posts: 29
Joined: Fri Jul 26, 2013 4:43 am

Re: in CefRenderHandler->OnPaint can save the buffer to a bi

Postby BabeleDunnit » Thu Oct 31, 2013 4:24 am

look at my post viewtopic.php?f=6&t=11113, the OnPaint stuff works perfectly :)
BabeleDunnit
Techie
 
Posts: 12
Joined: Mon Oct 07, 2013 8:39 am

Re: in CefRenderHandler->OnPaint can save the buffer to a bi

Postby ushka » Thu Nov 13, 2014 2:19 pm

Yes, you can save OnPaint bitmap or use it internaly as Gdibitmap. Below you can see some example code.
If you uncomment #define DUMP_BMP_TO_FILE all result BPM files would be stored in folder D:\\bmp.
Code: Select all
              Gdiplus::Bitmap* pBitMap;
                                BYTE* imgData;
            int BitCount = 24;
            int ByteCount = BitCount/8;

//#define DUMP_BMP_TO_FILE
#ifdef DUMP_BMP_TO_FILE

            static int counter = 0;

            counter++;

            std::stringstream fNameStream;
            fNameStream << "D:\\\\bmp\\" << std::setfill('0') << std::setw(3) << counter << ".bmp";
            std::ofstream file(fNameStream.str(), std::ios::out | std::ios::binary);


            if (file.is_open() == false) {
               return;
            }
#endif

            // declare bmp structures
            BITMAPFILEHEADER bmfh;
            BITMAPINFOHEADER info;

            // andinitialize them to zero
            memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) );
            memset ( &info, 0, sizeof (BITMAPINFOHEADER ) );

            // fill the fileheader with data
            bmfh.bfType = 0x4d42;       // 0x4d42 = 'BM'
            bmfh.bfReserved1 = 0;
            bmfh.bfReserved2 = 0;
            bmfh.bfOffBits = 0x36;      // number of bytes to start of bitmap bits

            // fill the infoheader
            info.biSize = sizeof(BITMAPINFOHEADER);
            info.biWidth = width;
            info.biHeight = height;
            info.biPlanes = 1;         // we only have one bitplane
            info.biBitCount = 24;      // RGB mode is 24 bits
            info.biCompression = BI_RGB;   
            info.biSizeImage = 0;      // can be 0 for 24 bit images
            info.biXPelsPerMeter = 0x0ec4;     // paint and PSP use this values
            info.biYPelsPerMeter = 0x0ec4;     
            info.biClrUsed = 0;         // we are in RGB mode and have no palette
            info.biClrImportant = 0;    // all colors are important



            unsigned long LineWidth = 0;
            if ((ByteCount % 4) != 0)
               LineWidth = ((width * BitCount + 31 ) & ~31) >> 3;
            else
               LineWidth = width;

            unsigned long paddedsize =  LineWidth * height;
            bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize;

            if(imgData){
               delete imgData;
               imgData = NULL;
            }

            imgData = new BYTE[paddedsize];
            memset ( imgData, 0, paddedsize );

            unsigned long padding = 0;
            unsigned long scanlinebytes = width * 4;
            while ( ( scanlinebytes + padding ) % (4) != 0 )     // DWORD = 4 bytes
               padding++;
            // get the padded scanline width
            unsigned long psw = scanlinebytes + padding;

            unsigned long bufpos = 0;   
            unsigned long newpos = 0;
            for ( long y = 0; y < height; y++ )
               for ( long x = 0, x1=0; x < ByteCount * width; x+=ByteCount, x1+=4 )
               {
                  newpos = y * LineWidth + x;     
                  bufpos = ( height - y - 1 ) * psw + x1;

                  imgData[newpos]     = ((BYTE*)buffer)[bufpos];       
                  imgData[newpos + 1] = ((BYTE*)buffer)[bufpos+1];
                  imgData[newpos + 2] = ((BYTE*)buffer)[bufpos + 2];
               }

#ifdef DUMP_BMP_TO_FILE
            file.write((char*) &bmfh, sizeof(BITMAPFILEHEADER));
            file.write((char*) &info, sizeof(BITMAPINFOHEADER));
            file.write((char*) ((void*)imgData), paddedsize);
            file.close();
#endif

            BITMAPINFO BmpInfo;

            memset(&BmpInfo.bmiHeader, 0, sizeof(BITMAPINFOHEADER));
            BmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            BmpInfo.bmiHeader.biBitCount = info.biBitCount;
            BmpInfo.bmiHeader.biWidth = info.biWidth;
            BmpInfo.bmiHeader.biHeight = info.biHeight;
            BmpInfo.bmiHeader.biPlanes = info.biPlanes;
            BmpInfo.bmiHeader.biCompression = info.biCompression;
            BmpInfo.bmiHeader.biSizeImage = info.biSizeImage;
            BmpInfo.bmiHeader.biXPelsPerMeter = info.biXPelsPerMeter;
            BmpInfo.bmiHeader.biYPelsPerMeter = info.biYPelsPerMeter;
            BmpInfo.bmiHeader.biClrUsed = info.biClrUsed;
            BmpInfo.bmiHeader.biClrImportant = info.biClrImportant;

            bool success = false;
            if (pBitMap) {
               delete(pBitMap);
               pBitMap = NULL;
            }
            pBitMap = Bitmap::FromBITMAPINFO(&BmpInfo, imgData);

            if(pBitMap)
               MessageBox(NULL,L"Get Bitmap Pointer Error!!!",L"FromBITMAPINFOTest",MB_OK);
ushka
Newbie
 
Posts: 1
Joined: Thu Nov 13, 2014 2:09 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 38 guests