【问题标题】:How to Convert byte array to ImageSource for Windows 8.0 store application如何将字节数组转换为 Windows 8.0 商店应用程序的 ImageSource
【发布时间】:2018-12-10 09:37:09
【问题描述】:

我正在开发 Windows 8 商店应用程序。我是新手。

我正在接收字节数组(byte [])形式的图像。

我必须将其转换回 Image 并在 Image Control 中显示。

到目前为止,我在屏幕上有按钮和图像控件。当我点击按钮时,我调用以下函数

private async Task LoadImageAsync()
{
    byte[] code = //call to third party API for byte array
    System.IO.MemoryStream ms = new MemoryStream(code);
    var bitmapImg = new Windows.UI.Xaml.Media.Imaging.BitmapImage();

    Windows.Storage.Streams.InMemoryRandomAccessStream imras = new Windows.Storage.Streams.InMemoryRandomAccessStream();

    Windows.Storage.Streams.DataWriter write = new Windows.Storage.Streams.DataWriter(imras.GetOutputStreamAt(0));
    write.WriteBytes(code);
    await write.StoreAsync();
    bitmapImg.SetSourceAsync(imras);
    pictureBox1.Source = bitmapImg;
}

这不能正常工作。任何想法? 当我调试时,我可以看到以 ms 为单位的字节数组。但它没有被转换为 bitmapImg。

【问题讨论】:

    标签: windows c#-4.0


    【解决方案1】:

    我在Codeproject找到以下内容

    public class ByteImageConverter
    {
        public static ImageSource ByteToImage(byte[] imageData)
        {
            BitmapImage biImg = new BitmapImage();
            MemoryStream ms = new MemoryStream(imageData);
            biImg.BeginInit();
            biImg.StreamSource = ms;
            biImg.EndInit();
    
            ImageSource imgSrc = biImg as ImageSource;
    
            return imgSrc;
        }
    }
    

    这段代码应该适合你。

    【讨论】:

    【解决方案2】:

    你可以试试这样的:

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        byte[] rawImage = value as byte[];
    
        using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
        {
            using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
            {
                writer.WriteBytes((byte[])rawImage);
    
                // The GetResults here forces to wait until the operation completes
                // (i.e., it is executed synchronously), so this call can block the UI.
                writer.StoreAsync().GetResults();
            }
    
            BitmapImage image = new BitmapImage();
            image.SetSource(ms);
            return image;
        }
    }
    

    【讨论】:

    • 我找到了您在此处使用的 DataWriter 类的命名空间,但我无法放置 using 语句...如果我需要特定的参考?
    • “我无法放置 using 语句”是什么意思?
    • 忽略我之前的评论。对不起。
    【解决方案3】:

    我在另一个帖子 (Image to byte[], Convert and ConvertBack) 中找到了以下答案。我在一个 Windows Phone 8.1 项目中使用了这个解决方案,不确定 Windows Store 应用程序,但我相信它会起作用。

    public object Convert(object value, Type targetType, object parameter, string culture)
        {
            // Whatever byte[] you're trying to convert.
            byte[] imageBytes = (value as FileAttachment).ContentBytes;
    
            BitmapImage image = new BitmapImage();
            InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
            ms.AsStreamForWrite().Write(imageBytes, 0, imageBytes.Length);
            ms.Seek(0);
    
            image.SetSource(ms);
            ImageSource src = image;
    
            return src; 
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多