使用 JpegBitmapDecoder 从字节数组加载图像时出现 ArgumentException

发布于 2024-10-30 14:46:22 字数 1401 浏览 10 评论 0原文

我在课堂上读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2024-11-06 14:46:22

您正在使用 Bitmap 构造函数,该构造函数通常用于加载已知格式(JPEG、PNG 等)的图像文件。相反,您只是获得了一堆字节,而您却没有告诉它任何关于您想要使用它们的格式的信息。

目前还不清楚您为什么要使用 BitmapDecoder 和 BitmapSource - 为什么不直接使用:

Stream imageStream = File.Open(this.FilePath, FileMode.Open,
                               FileAccess.Read, FileShare.Read));
this.bitmap = new Bitmap(imageStream);

请注意,您必须此处使用 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:

Stream imageStream = File.Open(this.FilePath, FileMode.Open,
                               FileAccess.Read, FileShare.Read));
this.bitmap = new Bitmap(imageStream);

Note that you mustn't use a using statement here - the Bitmap "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 :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文