簡體   English   中英

將 8 位 BMP 圖像轉換為 base64 字符串

[英]convert 8 bit BMP image to base64 String

在 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[])));

您將需要使用第三方庫 - 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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM