发送到 WCF 服务的 UWP 墨迹笔画。 Black/Blank 图片结果

UWP Ink strokes sent to WCF service. Black/Blank image result

我不知道怎么问这个。我有一个 UWP 应用程序,可以将墨迹笔划捕获为 BLOB,并将它们发送到服务,该服务将它们解码回 PNG 或其他图像格式。

原本墨迹效果很好。我能够捕获墨水并将其转换回图像。但最近我注意到墨水笔划仍然保存到 BLOB,只是我无法解码。

这些是我习惯看到的 BLOB 类型...

iVBORw0KGgoAAAANSUhEUgAAB0QAAAUvCAYAAAAhI1YPAAAAAXNSR0IArs4c[...]FOl/H86CXiFEsRU5ErkJggg==

这些是我现在看到的 BLOB...

R0lGODlhjgHCAHAAACH5BAEAAAMALAAAAACOAcIAgQAAA/wAA/gAAAAL/nI+py+0[...]AAAAAAAAKjUQlzgAAAAA7

我尝试在使用

时以各种格式类型保存图像

Convert.FromBase64String()

将 BLOB 转换为可管理的字节数组,但到目前为止没有任何效果。 我知道新的 BLOB 是有效的,因为一些在线 base64 解码器能够生成我期望的图像。有没有人见过这些类型的 BLOB 并知道如何正确解码它们?

更新(应要求)

    public static void SampleForWhosebug()
    {
        byte[] bytes = Convert.FromBase64String("R0lGODlhFgATAHAAACH5BAEAAAMALAAAAAAWABMAgQAAAAAA/gAA/wAAAAI1nI8jku262JtR0nWqia16DIHb542cSVohqJWJtk7Z6V7iZ6tkB88pzvv1gj8ZamhMKpdMQwEAIf7+APwGHQI+MAGQAUHz8wD2Xq9JmbmrFj3xo/prOzC0FlISTovE2rgnfpcOi5xAxOBymESobsE+RUdbqbyTWz0uFlZJn7lelx8qfs7F9U74cWifSZkoUdpAYWqcbZ8HKXZVEkaLBcUmKA0Ge1VU2H4I/0FAq4WFLM14HhihTILQiG6VQLRDsmlYT9TvL8nWZfDiqk2TFsUtPs+g3gMsSBVkBj8ACAACDRBlAhKIAGYCEogCRICA/AdFaGcCMpgAaAY+AE+pgAAARmgFBzg2C2lqa2wZHDIJAP7/BwEAwB5FMwkA/v8HAQDAHkU4BAD+/wEKjQVEg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IVzTPRsf9k8fnCyInYjRQQfv4rG7yrnx850ni885VzLrav2ASrkSsHjSTWdITxAk6jv44jCGJpm+hnOmsC+688pek9AJuQCoVPgMNJwAAAABJRU5ErkJggg==");
        Image image, image2;
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            image = Image.FromStream(ms, true);
        }
        using (MemoryStream ms2 = new MemoryStream(bytes2))
        {
            image2 = Image.FromStream(ms2, true);
        }
        image.Save("image.png");
        image2.Save("image2.png");

        if (image != null)
        { image.Dispose(); }
        if (image2 != null)
        { image2.Dispose(); }
    }

"R01GOD1" 字符串已转换为图像,然后使用站点“http://freeonlinetools24.com/base64-image

编码回 "iVBOR" 字符串

更新 2

这是我在 UWP 中使用的过程的代码示例,用于将 inkstrokecontainer 转换为 base64string,然后我可以将其发送到我的服务。

    private async static Task<string> WriteInkToBase64String(Windows.UI.Input.Inking.InkStrokeContainer inkContainer)
    {
        byte[] data;
        Stream s = null;
        MemoryStream ms = new MemoryStream();
        Windows.Storage.Streams.InMemoryRandomAccessStream imras = new Windows.Storage.Streams.InMemoryRandomAccessStream();
        try
        {
            await inkContainer.SaveAsync(imras);
            s = imras.AsStream();
            s.Position = 0;
            s.CopyTo(ms);
            data = ms.ToArray();
        }
        finally {
            ms.Dispose();
            if (s != null)
                s.Dispose();
            imras.Dispose();

        }
        return Convert.ToBase64String(data);
    }

要在 UWP 中解码 Base64 字符串,您可以使用 CryptographicBuffer class 来解码或编码数据。

IBuffer buffer = CryptographicBuffer.DecodeFromBase64String("R0l==")

然后,您可以使用以下代码将buffer转换为bitmapimage

using (IRandomAccessStream randomAccessStream = buffer2.AsStream().AsRandomAccessStream())
{
    var bitmapimage = new BitmapImage();
    await bitmapimage.SetSourceAsync(randomAccessStream);
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
     {
         image1.Source = bitmapimage;
     });
}

有关详细信息,您可以参考 Encode and decode data 文档。

这是解决方案。
两种 Base64 字符串格式都有效。 UWP 应用程序产生的最新格式只能读取为Bitmap。如果您尝试将 Ink Strokes 读入 Image,则会得到一个黑框。

public static Bitmap ConvertBase64ToBitmap(string base64String)
{
    byte[] stringBytes = Convert.FromBase64String(base64String);
    Bitmap bitmap;
    using (MemoryStream ms = new MemoryStream(stringBytes))
    {
        bitmap = new Bitmap(ms);
    }
    return bitmap;
}

如果您不想在 server/service 端处理位图,那么您可以将墨迹转换为图像并将其自由保存为字节数组或 base64 字符串或其他任何形式。

public static async Task<string> WriteInkAsPngToBase64(InkStrokeContainer ink)
{
    SoftwareBitmap sbmp;
    byte[] byteResults = null;
    string base64String = null;
    // inStream is the IRandomAccessStream with the GIF data (filled from InkStrokeContainer.SaveAsync)
    // outStream is an IRandomAcccessStream to save the PNG data
    //                                                                              -Rob Caplan - MSFT
    InMemoryRandomAccessStream inStream = new InMemoryRandomAccessStream();
    InMemoryRandomAccessStream outStream = new InMemoryRandomAccessStream();
    await ink.SaveAsync(inStream);
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(inStream);
    sbmp = await decoder.GetSoftwareBitmapAsync();
    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outStream);
    encoder.SetSoftwareBitmap(sbmp);

    await encoder.FlushAsync();
    byteResults = new byte[outStream.Size];
    using (DataReader reader = new DataReader(outStream.GetInputStreamAt(0)))
    {
        await reader.LoadAsync((uint)outStream.Size);
        reader.ReadBytes(byteResults);
    }
    base64String = Convert.ToBase64String(byteResults);
    return base64String;
}