Base64从编码到显示的图像

时间:2013-06-12 10:32:22

标签: javascript image visual-c++ base64 encode

我正在尝试通过插件在Visual C ++上显示的网页上显示图像。

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;
}

在我的函数中,你可以看到我得到一个BITMAPINFOHEADER(来自Twain),我从中创建了一个BITMAP,然后我使用了我在google上找到的base64编码器:http://www.adp-gmbh.ch/cpp/common/base64.html

我得到一个我给网页的字符串,我正在尝试

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

但它不起作用!

我将我得到的字符串与我的代码进行比较,以及我使用在线编码器获得的字符串,并且它与众不同,我的代码很短。

我做错了什么?标头问题?我不知道如何编码我的图像的标题?

1 个答案:

答案 0 :(得分:1)

您以错误的方式计算了图像大小。请尝试:

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