BytesToBitmap函数中把stream.Close();去掉
因为函数的返回值是new Bitmap(stream),这个返回值后来给了PicboxIMG,当再将图像转成2进制的时候,因为和PicboxIMG关联的MemoryStream已经被关闭,这样在Bitmap.Save(ms, Bitmap.RawFormat);这里就会出错。msdn对于new Bitmap(stream)中说过“在 Bitmap 的生存期内,必须使流保持打开。”

解决方案 »

  1.   

    但是还有些问题, 就是将BytesToBitmap函数中return 这句改为return new Bitmap((Image)new Bitmap(stream));之后不管去掉还是不去掉stream.Close();都在相同的位置Bitmap.Save(ms, Bitmap.RawFormat);有值不能为 null。参数名: encoderStackTrace:在 System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,EncoderParameters encoderParams)\r\n   
                在 System.Drawing.Image.Save(Stream stream, ImageFormat format)\r\n   
                在 IMGToBinary.IMGToBinaryHelper.BitmapToBytes(Bitmap Bitmap) 位置 E:\\WorkProject\\Test Project\\MyTest\\IMGToBinary\\IMGToBinary\\IMGToBinaryHelper.cs:行号 43" string
    在帮我看看,怎么回事,第一次就出现的这个错误,当我改过return那几后就出现刚解决的那个错误,帮我看看,如果解决我加分
      

  2.   

    但是还有些问题, 就是将BytesToBitmap函数中return 这句改为return new Bitmap((Image)new Bitmap(stream));之后不管去掉还是不去掉stream.Close();都在相同的位置Bitmap.Save(ms, Bitmap.RawFormat);有值不能为 null。参数名: encoderStackTrace:在 System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,EncoderParameters encoderParams)\r\n   
                在 System.Drawing.Image.Save(Stream stream, ImageFormat format)\r\n   
                在 IMGToBinary.IMGToBinaryHelper.BitmapToBytes(Bitmap Bitmap) 位置 E:\\WorkProject\\Test Project\\MyTest\\IMGToBinary\\IMGToBinary\\IMGToBinaryHelper.cs:行号 43" string
    在帮我看看,怎么回事,第一次就出现的这个错误,当我改过return那几后就出现刚解决的那个错误,帮我看看,如果解决我加分

    return new Bitmap((Image)new Bitmap(stream));会重新生成一个实例,但是Bitmap的默认的格式(Bitmap.RawFormat)不被Save函数支持,你可以手动设定一个格式。
    比如bmp.Save(ms, ImageFormat.Bmp);
      

  3.   

    但是还有些问题, 就是将BytesToBitmap函数中return 这句改为return new Bitmap((Image)new Bitmap(stream));之后不管去掉还是不去掉stream.Close();都在相同的位置Bitmap.Save(ms, Bitmap.RawFormat);有值不能为 null。参数名: encoderStackTrace:在 System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,EncoderParameters encoderParams)\r\n   
                在 System.Drawing.Image.Save(Stream stream, ImageFormat format)\r\n   
                在 IMGToBinary.IMGToBinaryHelper.BitmapToBytes(Bitmap Bitmap) 位置 E:\\WorkProject\\Test Project\\MyTest\\IMGToBinary\\IMGToBinary\\IMGToBinaryHelper.cs:行号 43" string
    在帮我看看,怎么回事,第一次就出现的这个错误,当我改过return那几后就出现刚解决的那个错误,帮我看看,如果解决我加分

    return new Bitmap((Image)new Bitmap(stream));会重新生成一个实例,但是Bitmap的默认的格式(Bitmap.RawFormat)不被Save函数支持,你可以手动设定一个格式。
    比如bmp.Save(ms, ImageFormat.Bmp);
    标准答案,呵呵,谢谢了。我加你好友啦