简体   繁体   中英

Base64 Image from encoding to display

I am trying to display an image on a web page that I get from Visual C++ via a pluggin.

std::string DIBtoURI(LPBITMAPINFOHEADER lpbmih){


    int width = lpbmih->biWidth;

   int height = lpbmih->biHeight; // height < 0 if bitmap is top-down
   if (height < 0)
       height = -height;


   // Populate the pixels array.

   unsigned char *bitmap = (unsigned char *) lpbmih +
                           sizeof(BITMAPINFOHEADER);


   HBITMAP f = CreateBitmap(width,height,1,24,bitmap);
   BITMAP bit;
   GetObject(f, sizeof(BITMAP), &bit);




   // Allocate memory.
   BYTE* pBitmapData = new BYTE[ bit.bmWidth*bit.bmHeight ];
    ZeroMemory( pBitmapData, bit.bmWidth*bit.bmHeight );

    // Get bitmap data.
    GetBitmapBits(f, bit.bmWidth*bit.bmHeight, pBitmapData );


    std::string tee = base64_encode(pBitmapData,bit.bmWidth*bit.bmHeight);

   return tee;
}

In my function, you can see I get a BITMAPINFOHEADER (from Twain), I create a BITMAP from it, and then I use a base64 encoder I have found on google : http://www.adp-gmbh.ch/cpp/common/base64.html

I get a string I give to the web page and I'm trying to do

var src="image/jpeg;base64," + string_from_cpp

But it doesn't work !

I compared the string I get with my code, and what I get with a online encoder, and it's different, mine is very short.

What am I doing wrong? Header problem ? I don't know how to encode the header of my image ?

You calculated the image size in the wrong way. Please try:

((((bit.bmWidth * 24)+ 31)/32)*4)*bit.bmHeight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM