将UWP墨水笔划发送到WCF服务。黑白图像结果

时间:2018-08-31 15:54:59

标签: uwp base64 blob stroke

我不确定该怎么问。我有一个UWP应用程序,可将笔划捕获为BLOB,并将其发送到服务,然后将其解码回PNG或其他图像格式。

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

这些是我经常看到的BLOB类型...

(defun args ()
  #+CCL CCL:*UNPROCESSED-COMMAND-LINE-ARGUMENTS*
  #+SBCL (cdr *posix-argv*))

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

iVBORw0KGgoAAAANSUhEUgAAB0QAAAUvCAYAAAAhI1YPAAAAAXNSR0IArs4c[...]FOl/H86CXiFEsRU5ErkJggg==

我尝试在使用

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

Convert.FromBase64String()

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

更新(按要求)

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

enter image description here enter image description here

已将“ R01GOD1”字符串转换为图像,然后使用站点“ http://freeonlinetools24.com/base64-image”将其编码回“ iVBOR”字符串

更新2

这是我在UWP中使用的将墨水笔迹容器转换为base64字符串然后可以发送到我的服务的过程的代码示例。

    public static void SampleForStackOverflow()
    {
        byte[] bytes = Convert.FromBase64String("R0lGODlhFgATAHAAACH5BAEAAAMALAAAAAAWABMAgQAAAAAA/gAA/wAAAAI1nI8jku262JtR0nWqia16DIHb542cSVohqJWJtk7Z6V7iZ6tkB88pzvv1gj8ZamhMKpdMQwEAIf7+APwGHQI+MAGQAUHz8wD2Xq9JmbmrFj3xo/prOzC0FlISTovE2rgnfpcOi5xAxOBymESobsE+RUdbqbyTWz0uFlZJn7lelx8qfs7F9U74cWifSZkoUdpAYWqcbZ8HKXZVEkaLBcUmKA0Ge1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2 = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAABYAAAATCAYAAACUef2IAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAACJSURBVDhP3ZFbCsAgDAS197+zRRLw0V2TqF8dKIKYcd2mGKXIZ5N1dTAL83L20fU60619qj4RS8vOD2JfdwISV5pcq4hIVzTPRsf9k8fnCyInYjRQQfv4rG7yrnx850ni885VzLrav2ASrkSsHjSTWdITxAk6jv44jCGJpm+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(); }
    }

2 个答案:

答案 0 :(得分:0)

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

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文档。

答案 1 :(得分:0)

这是解决方案。
两种Base64字符串格式均可使用。 UWP应用程序产生的最新格式只能读取为Bitmap。如果您尝试以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;
}

如果您不想在服务器/服务端处理位图,则可以将墨水转换为图像,然后将其自由保存为字节数组或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;
}