为什么我在处理 jpeg 图像字节流时会遇到很多异常

Why am I getting alot of exceptions processing jpeg image byte streams

我正在尝试使用 Aspose.Imaging 版本 21.7.0 来压缩一些 .jpg 和 .png 字节数组。

我的 Winforms 点击事件:

    private void btnConvert_Click(object sender, EventArgs e)
    {
        _context.Images.Load();

        var images = _context.Images.Local.ToBindingList();
        
        foreach (var image in images)
        {
            if (image.ImageData != null)
            {
                image.MimeType = MimeTypes.GetMimeType(image.Filename.Trim());
                if (image.MimeType.StartsWith("image") && image.MimeType != "image/vnd.microsoft.icon")
                {

                    byte[] newImage;
                    using (MemoryStream stream = new MemoryStream(image.ImageData))
                    {
                        using (RasterImage myImage = (RasterImage) Image.Load(stream))
                        {
                            switch (image.MimeType)
                            {
                                case "image/png":
                                    PngOptions options = new PngOptions
                                    {
                                        CompressionLevel = 9,
                                        ColorType = PngColorType.IndexedColor,
                                        Progressive = true,
                                        Palette = ColorPaletteHelper.GetCloseImagePalette(myImage, 256,
                                            PaletteMiningMethod.Histogram)
                                    };

                                    using (MemoryStream memory = new MemoryStream())
                                    {
                                        myImage.Save(memory, options);
                                        memory.Position = 0;
                                        newImage = memory.ToArray();
                                    }

                                    break;
                                case "image/jpeg":
                                    JpegOptions saveOptions = new JpegOptions
                                    {
                                        BitsPerChannel = 8,
                                        CompressionType = JpegCompressionMode.Progressive,
                                        Quality = 100,
                                        ResolutionSettings = new ResolutionSetting(96.0, 96.0),
                                        ResolutionUnit = ResolutionUnit.Inch,
                                        ColorType = JpegCompressionColorMode.Cmyk,
                                        Palette = ColorPaletteHelper.GetCloseImagePalette(myImage, 256, PaletteMiningMethod.Histogram)
                                    };

                                    using (MemoryStream memory = new MemoryStream())
                                    {
                                        Debug.WriteLine(image.Filename);
                                        try
                                        {
                                            myImage.Save(memory, saveOptions); <<== Exceptions
                                        }
                                        catch (Exception exception)
                                        {
                                            Debug.WriteLine("Image Byte Array: " + image.ImageData);
                                        }
                                        
                                        memory.Position = 0;
                                        newImage = memory.ToArray();
                                    }

                                    break;
                                default:
                                    newImage = null;
                                    break;
                            }
                            
                        }
                    }

                    image.CompressedImageData = Compress(newImage);
                }
                else
                {
                    image.CompressedImageData = null;
                }
            }
        }

        var count = _context.SaveChanges();

        MessageBox.Show($@"{count} images compressed.");
    }

我收到很多关于 jpeg 图像的异常。示例:

Exception thrown: '   ' in Aspose.Imaging.dll

Exception thrown: 'Aspose.Imaging.FileFormats.Dng.DngException' in Aspose.Imaging.dll

Exception thrown: '   ' in Aspose.Imaging.dll

Exception thrown: 'Aspose.Imaging.CoreExceptions.ImageSaveException' in Aspose.Imaging.dll

...全部从 jpeg 部分的 myImage.Save(memory, saveOptions) 抛出。

我们将不胜感激。

由于Aspose.Imaging是付费产品, 您应该在他们的网站上联系他们以获得支持。

有专门针对Aspose产品的免费论坛: https://forum.aspose.com/

遇到过 Aspose 错误的人都知道,由于代码的混淆,这些错误很难理解,所以最好的解决办法是直接与他们联系。