在'image.SetSource(stream);'处将字节数组转换为位图错误未指定的错误 [英] Converting byte array to bitmap error at 'image.SetSource(stream);' Unspecified error

查看:79
本文介绍了在'image.SetSource(stream);'处将字节数组转换为位图错误未指定的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码将Image转换为Byte数组

 public static byte [] ConvertStringToBytes(string input)
{
MemoryStream stream = new的MemoryStream();

使用(StreamWriter writer = new StreamWriter(stream))
{
writer.Write(input);
writer.Flush();
}

return stream.ToArray();
}





现在我想将该字节数组重新转换为Imagebitmap,我在下面使用了代码



 public BitmapImage ByteArraytoBitmap(Byte [] byteArray)
{
MemoryStream stream = new MemoryStream(byteArray);

byte [] bytes = byteArray as byte [];

BitmapImage image = new BitmapImage();

image.SetSource(stream);

返回图片;
}





但它在'image.SetSource(stream);'未指定错误

解决方案

好的......它会的。



但不是从字节数组到图像的转换就是问题,它是另一种方法。



看代码:字符串中有什么?如果是图像数据,则可能因转换为字符串而损坏。但更可能的是,它是一个文件名或类似的,而不是实际的图像数据...



使用调试器,并查看字符串。

I used below code to convert Image to Byte array

public static byte[] ConvertStringToBytes(string input)
        {
            MemoryStream stream = new MemoryStream();

            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.Write(input);
                writer.Flush();
            }

            return stream.ToArray();
        }



Now I want to re convert that byte array to Imagebitmap, i used below code

public BitmapImage ByteArraytoBitmap(Byte[] byteArray)
        {
            MemoryStream stream = new MemoryStream(byteArray);           

            byte[] bytes = byteArray as byte[];

            BitmapImage image = new BitmapImage();

            image.SetSource(stream);

            return image;
        }



But it gives error at 'image.SetSource(stream);' Unspecified error

解决方案

Well yes... It will.

But it's not the conversion from byte array to image that's the problem, it's the other method.

Look at the code: what is in the string? If it's image data, then it's probably corrupt from being converted to a string. But more likely, it's a file name or similar, rather than actual image data at all...

Use the debugger, and look at the string.


这篇关于在'image.SetSource(stream);'处将字节数组转换为位图错误未指定的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆