我有一个非常奇怪的错误。我需要缩小图像,缩小质量并转换为 JPEG。当我将文件保存在磁盘上时,这一切都有效,但是当我将它保存到流时它不起作用。
System.Drawing.Bitmap bitmap = // valid Bitmap from Disk
System.IO.Stream stream = new MemoryStream();
// JPEG Encoding
System.Drawing.Imaging.ImageCodecInfo jpgEncoder = GetEncoder( System.Drawing.Imaging.ImageFormat.Jpeg );
System.Drawing.Imaging.Encoder encoder2 = System.Drawing.Imaging.Encoder.Quality;
System.Drawing.Imaging.EncoderParameters parameters = new System.Drawing.Imaging.EncoderParameters( 1 );
System.Drawing.Imaging.EncoderParameter parameter = new System.Drawing.Imaging.EncoderParameter( encoder2, qualityLevel );
parameters.Param[0] = parameter;
// Save downscaled on Disk and stream
bitmap.Save( stream, jpgEncoder, parameters );
bitmap.Save( @"C:\TestJPEG.jpg", jpgEncoder, parameters );
// some stream stuff
var bytes = ((MemoryStream)stream).ToArray();
System.IO.Stream inputStream = new MemoryStream( bytes );
// Load from disk and stream
Bitmap fromDisk = new Bitmap( @"C:\TestJPEG.jpg" ); // works
Bitmap fromStream = new Bitmap( inputStream ); // crash invalid parameter no inner message or description
Bitmap fromStream2 = (Bitmap)Bitmap.FromStream( inputStream ); // same error here
// also crashes if I load the "stream" named Stream
我也可以用 Paint 打开转换后的文件。有什么建议么?
编辑: 我在 Windows 7 Professional 上使用 .Net Framework 4.0
Edit2: 尝试了 Seek 的事情(答案已被删除)
stream.Seek( 0, SeekOrigin.Begin );
它适用于“旧”流。但我需要从字节数组中加载它。还是一样的崩溃