将8位BMP图像转换为base64字符串

时间:2017-01-12 13:55:14

标签: c# base64 bmp

在C#中,我使用以下代码从硬盘读取BMP图像,然后将其转换为字节数组,然后将数组转换为base64字符串。

我的问题是如何在c ++中这样做?图像为8位深度

这是我的c#代码

System.Drawing.Image temp = System.Drawing.Image.FromFile(path);
System.Drawing.ImageConverter converter = new ImageConverter();
String imgString = Convert.ToBase64String((byte[])converter.ConvertTo(temp, typeof(byte[])));

1 个答案:

答案 0 :(得分:0)

你需要使用第三方库 - 在C ++中没有像.NET那样的“标准”base64编码。

C ++中有base64编码的链接和代码段:base64 decode snippet in c++

使用由Bauss链接的base64库的以下(未经测试的)代码应该可以满足您的需求。

#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

#include "base64.h" //http://www.adp-gmbh.ch/cpp/common/base64.html

using namespace std;
string encodeFile(string file)
{
  ifstream input( file, ios::binary );
  vector<char> rawData(istreambuf_iterator<char>(input), istreambuf_iterator<char>());
  return base64_encode(&rawData[0], rawData.size());
}
相关问题