我怀疑在您的实际代码中,您正在写入 MemoryStream 而不是倒带它; 如果是这种情况,请在尝试重新加载之前将 Position 设置为 0。
I suspect that in your real code you are writing to a MemoryStream and not rewinding it; if this is the case, set Position to 0 before you try to re-load it.
private void Form1_Load(object sender, EventArgs e)
{
byte[] data = File.ReadAllBytes("c:\\t.jpg");
using (Stream originalBinaryDataStream = new MemoryStream(data))
{
// This works perfectly fine, if use this method (which i can't).
//image = new Bitmap("Chick.jpg");
// This throws an exception when it's deserialized.
// It doesn't like the memory stream reference?
originalBinaryDataStream.Seek(0, SeekOrigin.End);
pictureBox1.Image= new Bitmap(originalBinaryDataStream);
}
}
我在 PictureBox 中看到了图像。
Just tested your code, it worked, the code is fine. There must be a problem with the image file or path. This is my test:
private void Form1_Load(object sender, EventArgs e)
{
byte[] data = File.ReadAllBytes("c:\\t.jpg");
using (Stream originalBinaryDataStream = new MemoryStream(data))
{
// This works perfectly fine, if use this method (which i can't).
//image = new Bitmap("Chick.jpg");
// This throws an exception when it's deserialized.
// It doesn't like the memory stream reference?
originalBinaryDataStream.Seek(0, SeekOrigin.End);
pictureBox1.Image= new Bitmap(originalBinaryDataStream);
}
}
我已经更新了最初的问题帖子,其中包含整个 VS 解决方案的链接(这是一个类和一个单元测试)。 单元测试抛出失败失败异常。 请检查一下。
I've updated the initial question post with a link the entire VS solution (which is one class and one unit test). The unit test throws the fail fail fail exception. Please check it out.
发布评论
评论(3)
我怀疑在您的实际代码中,您正在写入
MemoryStream
而不是倒带它; 如果是这种情况,请在尝试重新加载之前将 Position 设置为 0。I suspect that in your real code you are writing to a
MemoryStream
and not rewinding it; if this is the case, set Position to 0 before you try to re-load it.刚刚测试了您的代码,它有效,代码很好。 一定是图片文件或者路径有问题。
这是我的测试:
我在 PictureBox 中看到了图像。
Just tested your code, it worked, the code is fine. There must be a problem with the image file or path.
This is my test:
And I see the image in the PictureBox.
我已经更新了最初的问题帖子,其中包含整个 VS 解决方案的链接(这是一个类和一个单元测试)。 单元测试抛出失败失败异常。 请检查一下。
I've updated the initial question post with a link the entire VS solution (which is one class and one unit test). The unit test throws the fail fail fail exception. Please check it out.