正如标题所说,我有一个例外的问题。 “System.ArgumentException”。我得到它时,我试图将字节数组转换为位图图像。这里是抛出异常的代码:参数无效内存流。 image.FromStream C#
MemoryStream ms = new MemoryStream(Buffer);
ms.Position = 0;
bmp = new Bitmap(ms);
pictureBox1.Image = bmp;
我也试过用图像转换器。而改为bmp = new Bitmap(ms);
使用此bmp = (Bitmap)image.FromStream(ms);
有趣的是,如果我会把任何这些(无论是与内存流或图像转换器)内的try catch块。它会创建图像,但它往往会遗漏一些部分(例如图像的一半是可见的),但有时它会创建完整的图像,并且在输出中它仍然会说异常已被抛出..并引发异常。 - 对不起语法。 - 是的,我没有尝试寻找我的问题的解决方案,但我没有找到任何东西
编辑: 看起来我忘了一些部分问题所以我这里做。我正在做项目,将桌面图像发送到另一个项目,并为此,它需要发送字节...如果我会把那File.WriteAllBytes
测试到第一个示例代码ive添加失败。但在发送它的一方工作。这是获取桌面的图像变成EM的字节代码
using (var stream = new MemoryStream())
{
bmp.Save(stream, ImageFormat.Jpeg);
Buffer = stream.ToArray();
}
而在堆栈跟踪它说的例外是在System.Drawing.Bitmap..ctor(流流)被抛出..
编辑2: 接收缓冲器:
int ArraySize = acc.Receive(Buffer, 0, Buffer.Length, 0);
Array.Resize(ref Buffer, ArraySize);
MemoryStream ms = new MemoryStream(Buffer);
ms.Position = 0;
bmp = new Bitmap(ms);
pictureBox1.Image = bmp; //Dont mind the naming
发送缓冲器:
bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, bmp.Size);
using (var stream = new MemoryStream())
{
bmp.Save(stream, ImageFormat.Jpeg);
stream.Flush();
acc.Send(stream.ToArray());
}//Dont mind the naming
当然还有插座之间的背景连接等。
字节数组中有什么? – adv12
您可能想要共享创建“缓冲区”的代码,该问题很可能源于此处。 –
作为正确性的快速检查,您可以使用适当的文件扩展名来调用'File.WriteAllBytes(filename,byteArray)'。如果打开文件时文件乱了,那么你的字节数组内容有问题。 – JamieSee