How can I create an Image in GDI+ from a Base64-Encoded string in C++?(如何从 C++ 中的 Base64 编码字符串在 GDI+ 中创建图像?)
问题描述
我有一个应用程序,目前是用 C# 编写的,它可以采用 Base64 编码的字符串并将其转换为图像(在本例中为 TIFF 图像),反之亦然.在 C# 中,这实际上非常简单.
I have an application, currently written in C#, which can take a Base64-encoded string and turn it into an Image (a TIFF image in this case), and vice versa. In C# this is actually pretty simple.
private byte[] ImageToByteArray(Image img)
{
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
return ms.ToArray();
}
private Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(byteArrayIn);
Image returnImage = Image.FromStream(ms, true, false);
return returnImage;
}
// Convert Image into string
byte[] imagebytes = ImageToByteArray(anImage);
string Base64EncodedStringImage = Convert.ToBase64String(imagebytes);
// Convert string into Image
byte[] imagebytes = Convert.FromBase64String(Base64EncodedStringImage);
Image anImage = byteArrayToImage(imagebytes);
(而且,现在我正在研究它,可以进一步简化)
(and, now that I'm looking at it, could be simplified even further)
我现在有业务需要在 C++ 中执行此操作.我正在使用 GDI+ 绘制图形(目前仅适用于 Windows),并且我已经有代码来 解码 C++ 中的字符串(到另一个字符串).然而,我遇到的问题是将信息放入 GDI+ 中的 Image 对象中.
I now have a business need to do this in C++. I'm using GDI+ to draw the graphics (Windows only so far) and I already have code to decode the string in C++ (to another string). What I'm stumbling on, however, is getting the information into an Image object in GDI+.
在这一点上,我认为我需要任何一个
At this point I figure I need either
a) 一种将 Base64 解码字符串转换为 IStream 以提供给 Image 对象的 FromStream 函数的方法
a) A way of converting that Base64-decoded string into an IStream to feed to the Image object's FromStream function
b) 一种将 Base64 编码的字符串转换为 IStream 以提供给 Image 对象的 FromStream 函数的方法(因此,与我当前使用的代码不同)
b) A way to convert the Base64-encoded string into an IStream to feed to the Image object's FromStream function (so, different code than I'm currently using)
c) 一些完全不同的方式我在这里没有想到.
c) Some completely different way I'm not thinking of here.
我的 C++ 技能非常生疏了,而且我也被托管的 .NET 平台宠坏了,所以如果我攻击这一切都是错误的,我愿意接受建议.
My C++ skills are very rusty and I'm also spoiled by the managed .NET platform, so if I'm attacking this all wrong I'm open to suggestions.
更新:除了我在下面发布的解决方案之外,我还想出了如何如果有人需要的话,可以另辟蹊径.
UPDATE: In addition to the solution I've posted below, I've also figured out how to go the other way if anyone needs it.
推荐答案
这应该是一个两步的过程.首先,将 base64 解码为纯二进制(如果您从文件中加载 TIFF,您将拥有的位).第一个 Google 结果 看起来不错.
This should be a two-step process. Firstly, decode the base64 into pure binary (the bits you would have had if you loaded the TIFF from file). The first Google result for this looks to be pretty good.
其次,您需要将这些位转换为位图对象.当我必须从资源表加载图像时,我遵循 this example.
Secondly, you'll need to convert those bits to a Bitmap object. I followed this example when I had to load images from a resource table.
这篇关于如何从 C++ 中的 Base64 编码字符串在 GDI+ 中创建图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 C++ 中的 Base64 编码字符串在 GDI+ 中创建图
基础教程推荐
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 设计字符串本地化的最佳方法 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04