-4

In C# I use the following code in order to read BMP image from hard disk then convert it into byte array then convert the array to base64 string.

my question is how to do that in c++? The image is 8 bit depth

here is my c# code

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[])));
Richard Anthony Hein
  • 10,550
  • 3
  • 42
  • 62
Mohammad Shaban
  • 176
  • 1
  • 16
  • 2
    Same as in C#. Read file, encode using http://www.adp-gmbh.ch/cpp/common/base64.html and done. – jamek Jan 12 '17 at 13:58
  • 2
    @RawN Did you mean remove the C# tag? OP wants to do that in C++. – Borgleader Jan 12 '17 at 14:08
  • http://www.adp-gmbh.ch/cpp/common/base64.html – Bauss Jan 12 '17 at 14:20
  • That's rather strange c# code, though. What's the use of reading something as image just to save it back as bytes without any type conversion? Seems a lot more efficient to just use `File.ReadAllBytes()` and not bother with the temp image or the image converter at all. – Nyerguds Jan 09 '18 at 09:13

1 Answers1

0

You'll need to use third party libraries - there is no "standard" base64 encoding in C++ like there is in .NET.

There are links and a snippet on base64 encoding in C++ here: base64 decode snippet in c++

The following (untested) code using the base64 library linked to by Bauss, should do what you want.

#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());
}
Community
  • 1
  • 1
Richard Matheson
  • 1,125
  • 10
  • 24