我怎样才能创建GDI +从C ++一个Base64恩codeD字符串图像?

问题描述:

我有一个应用程序,目前用C#编写,这可能需要一个Base64-CN codeD字符串,并把它变成一个图像(在这种情况下TIFF图像),反之亦然。在C#其实这是pretty简单。

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目前为止),我已经有code到的德code 在C ++中的字符串(另一个字符串)。我跌跌撞撞什么,但是,是获取信息到GDI +图像对象。

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编码德codeD串入一个IStream喂到图像对象的FromStream功能的一种方式。

a) A way of converting that Base64-decoded string into an IStream to feed to the Image object's FromStream function

二)为Base64恩codeD字符串转换成一个IStream的一种方法喂养到图像对象的FromStream功能(因为如此,不同code比我目前使用)

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.

更新:除了我下面张贴的解决方案,我也想通了如何go另一种方式如果有人需要它。

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.

这应该是一个两个步骤的过程。首先,德code为Base64为纯二进制(如果加载从文件中的TIFF,你将不得不位)。 href=\"http://www.adp-gmbh.ch/cpp/common/base64.html\" rel=\"nofollow\">首款谷歌结果这样做的

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.

其次,你需要将这些位转换为位图对象。我跟着这个例子的,当我不得不加载从图像资源表。

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.