请高人看一下,哪出错了private void button1_Click(object sender, System.EventArgs e)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
pictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] FileByteArray = new Byte[ms.Length];
ms.Read(FileByteArray,0,(int)ms.Length);
ms.Close(); System.IO.MemoryStream ms2 = new System.IO.MemoryStream(FileByteArray);
Image myImage = Image.FromStream(ms2);
pictureBox2.Image = myImage;
ms2.Close();
}这是我的一段测试代码, 目的是想把一个Image 以二进制文件形式存储后,再读出来,看是否正确显示,结测试发现错误“未处理的“System.ArgumentException”类型的异常出现system.drawing.dll 中。其他信息: 使用了无效参数。”谁能帮我看看,哪里出了错误

解决方案 »

  1.   

    change
    System.IO.MemoryStream ms2 = new System.IO.MemoryStream(FileByteArray);with
    System.IO.MemoryStream ms2 = new System.IO.MemoryStream(FileByteArray, true);
    ms2.Read(FileByteArray,0,FileByteArray.Length);
      

  2.   

    Knight94(愚翁)
    按你方式改完,还是提示我同样的错误!我自己把内存流改成文件流,就不会正常执行,代码如下:
    private void button1_Click(object sender, System.EventArgs e)
    {

    System.IO.FileStream fs = new System.IO.FileStream(@"C:\Test.jpg",System.IO.FileMode.Create);
    pictureBox1.Image.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg);
    Byte[] FileByteArray = new Byte[fs.Length];
    fs.Read(FileByteArray,0,(int)fs.Length);
    fs.Close(); System.IO.FileStream fs2 = new System.IO.FileStream(@"C:\Test.jpg",System.IO.FileMode.Open);
    pictureBox2.Image = Image.FromStream(fs2);

    为什么用内存流就不好使, 问题出在哪?
      

  3.   

    你看看用memorystream存的时候,byte中是否有数据。
      

  4.   

    刚刚测试通过的,还是用的文件流,代码如下
    System.IO.FileStream fs = new System.IO.FileStream(@"C:\Test.jpg",System.IO.FileMode.Create);
    pictureBox1.Image.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg);
    fs.Close();
    fs = new System.IO.FileStream(@"C:\Test.jpg",System.IO.FileMode.Open);
    Byte[] FileByteArray = new Byte[fs.Length];
    fs.Read(FileByteArray,0,(int)fs.Length);
    fs.Close();System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
    ms2.Write(FileByteArray,0,FileByteArray.Length);Image myImage = Image.FromStream(ms2);
    pictureBox2.Image = myImage;
    ms2.Close();
      

  5.   

    用我上面这段代码, byte 中就有数据了, 怎样把我上面使用文件流的部分改成内存流呢???
      

  6.   

    在用MemoryStream来save的时候,并没有执行真正的写操作。
      

  7.   


    pictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
    之后加上
    ms.Flush();
      

  8.   

    搞定了!  :)
    Save的时候 执行真正的操作了, 但是在用 内存流的 Read 方法时,没有把数据写进我的数组,内存流提供了一个 GetBuffer方法,用这个方法可以得到我想要的数组, 问题解决了!谢谢你 Knight94(愚翁)结贴!