使用 JpegBitmapDecoder 从字节数组加载图像时出现 ArgumentException
我在课堂上读取 JPEG 文件时遇到一些问题。我需要从 JPEG 文件加载元数据和位图。到目前为止,我有这个:
public void Load()
{
using (Stream imageStream = File.Open(this.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BitmapDecoder decoder = new JpegBitmapDecoder(imageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource source = decoder.Frames[0];
// load metadata
this.metadata = source.Metadata as BitmapMetadata;
// prepare buffer
int octetsPerPixel = source.Format.BitsPerPixel / 8;
byte[] pixelBuffer = new byte[source.PixelWidth * source.PixelHeight * octetsPerPixel];
source.CopyPixels(pixelBuffer, source.PixelWidth * octetsPerPixel, 0);
Stream pixelStream = new MemoryStream(pixelBuffer);
// load bitmap
this.bitmap = new Bitmap(pixelStream); // throws ArgumentException
}
this.status = PhotoStatus.Loaded;
}
但是当尝试从流创建 Bitmap 实例时,Bitmap 构造函数会抛出 ArgumentException。
文档说:
System.ArgumentException
流不包含图像数据或为空。
-或-
流包含单个尺寸大于 65,535 像素的 PNG 图像文件。
我不确定,我做错了什么。你能帮我吗?
I'm having some trouble reading JPEG files in my class. I need to load metadata and bitmap from a JPEG file. So far, I have this:
public void Load()
{
using (Stream imageStream = File.Open(this.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BitmapDecoder decoder = new JpegBitmapDecoder(imageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource source = decoder.Frames[0];
// load metadata
this.metadata = source.Metadata as BitmapMetadata;
// prepare buffer
int octetsPerPixel = source.Format.BitsPerPixel / 8;
byte[] pixelBuffer = new byte[source.PixelWidth * source.PixelHeight * octetsPerPixel];
source.CopyPixels(pixelBuffer, source.PixelWidth * octetsPerPixel, 0);
Stream pixelStream = new MemoryStream(pixelBuffer);
// load bitmap
this.bitmap = new Bitmap(pixelStream); // throws ArgumentException
}
this.status = PhotoStatus.Loaded;
}
But the Bitmap constructor throws an ArgumentException when trying to create a Bitmap instance from a stream.
The documentation says:
System.ArgumentException
stream does not contain image data or is null.
-or-
stream contains a PNG image file with a single dimension greater than 65,535 pixels.
I'm not sure, what I did wrong. Can you please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用 Bitmap 构造函数,该构造函数通常用于加载已知格式(JPEG、PNG 等)的图像文件。相反,您只是获得了一堆字节,而您却没有告诉它任何关于您想要使用它们的格式的信息。
目前还不清楚您为什么要使用 BitmapDecoder 和 BitmapSource - 为什么不直接使用:
请注意,您必须此处使用
using
语句 - 在调用构造函数后,Bitmap
“拥有”流。除了所有这些之外,您似乎正在尝试混合 WPF 和 WinForms 的图像想法,我怀疑这通常是一个坏主意:(
You're using the
Bitmap
constructor which is usually used to load an image file in a known format - JPEG, PNG etc. Instead, you've just got a bunch of bytes, and you're not telling it anything about the format you want to use them in.It's not clear why you want to use BitmapDecoder and BitmapSource at all - why aren't you just using:
Note that you mustn't use a
using
statement here - theBitmap
"owns" the stream after you've called the constructor.Aside from all of this, you seem to be trying to mix WPF and WinForms ideas of images, which I suspect is a generally bad idea :(