简体   繁体   English

如何从 Base64 (UWP) 中的字符串创建图像

[英]How to create Image from string in Base64 (UWP)

I am making a Note application where I can draw with Windows ink and also paste images.我正在制作一个笔记应用程序,我可以在其中使用 Windows 墨水进行绘制并粘贴图像。 I want to save the images and ink in a single file.我想将图像和墨水保存在一个文件中。 Therefore I am converting the image to a string in base64 format so that I can easily serialize it.因此,我将图像转换为 base64 格式的字符串,以便我可以轻松地对其进行序列化。 My problem is when I try to recreate the image from the string.我的问题是当我尝试从字符串重新创建图像时。

My code to decode the image data into a base64 string:我将图像数据解码为 base64 字符串的代码:

var decoder = await BitmapDecoder.CreateAsync(imageStream);
var pixels = await decoder.GetPixelDataAsync();
var bytes = pixels.DetachPixelData();

base64String = Convert.ToBase64String(bytes);

My code to encode the base64 string into an image again ( This code does NOT work! The image is not added to the redCanvas ):我的代码将 base64 字符串再次编码为图像(此代码不起作用!图像未添加到 redCanvas ):

var bytes = Convert.FromBase64String(base64String);

BitmapImage bitmapImage = new BitmapImage();
using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
{
    await stream.WriteAsync(bytes.AsBuffer());
    stream.Seek(0);
    bitmapImage.SetSource(stream);
}
Image image = new Image();

image.Source = bitmapImage;

// Add the image to a second canvas
redCanvas.Children.Add(image);

Full code for pasting the image and creating the base64 string:粘贴图像和创建 base64 字符串的完整代码:

private async Task myButton_ClickAsync(object sender, RoutedEventArgs e)
{
    var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
    if (dataPackageView.Contains(StandardDataFormats.Bitmap))
    {
        IRandomAccessStreamReference imageReceived = null;
        imageReceived = await dataPackageView.GetBitmapAsync();
        if (imageReceived != null)
        {
            using (var imageStream = await imageReceived.OpenReadAsync())
            {
                var bitmapImage = new BitmapImage();
                bitmapImage.SetSource(imageStream);

                var decoder = await BitmapDecoder.CreateAsync(imageStream);
                var pixels = await decoder.GetPixelDataAsync();
                var bytes = pixels.DetachPixelData();

                base64String = Convert.ToBase64String(bytes);

                TextBox_Image_Base64.Text = base64;
         
                Image image = new Image();
                        
                // Add the image to a list of Images
                ListImages.Add(image);

                blueCanvas.Children.Add(image);

                image.Source = bitmapImage;

                TextBlock_Status.Text = "Status : Image is retrieved from the 
                                                 clipboard and pasted successfully.";
            }
        }
    }
    else
    {
        TextBlock_Status.Text = "Status : Bitmap format is not available in clipboard";
    }
}

Anyone have any suggestions as to how I can recreate the image from the string?有人对我如何从字符串重新创建图像有任何建议吗?

Screenshot from my app showing that I am able to paste an image and decode it into a string我的应用程序的屏幕截图显示我能够粘贴图像并将其解码为字符串

How to create Image from string in Base64 (UWP)如何从 Base64 (UWP) 中的字符串创建图像

We suggest you convet stream buffer to Base64 from imageStream diteactly, but not from PixelData.我们建议您将 stream 缓冲区直接从 imageStream 转换为 Base64,而不是从 PixelData。 you could use CryptographicBuffer class to approach.您可以使用CryptographicBuffer class 来接近。

using (var imageStream = await imageReceived.OpenReadAsync())
{
    var buffer = new Windows.Storage.Streams.Buffer((uint)imageStream.Size);

    await imageStream.ReadAsync(buffer, (uint)imageStream.Size, InputStreamOptions.None);

    String strBase64New = CryptographicBuffer.EncodeToBase64String(buffer);

   
}

Retrive from base64 string从 base64 字符串中检索

public static async Task<BitmapImage> LoadBase64(string base64)
{
    byte[] bytes = Convert.FromBase64String(base64);
    var bitmap = new BitmapImage();
    using (MemoryStream ms = new MemoryStream(bytes))
    {
        await bitmap.SetSourceAsync(ms.AsRandomAccessStream());
    }
    return bitmap;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM