【问题标题】:BadImage Error When Loading Tiff CCITTv4 Via SharpDX.Direct2D1.Bitmap.FromWicBitmap通过 SharpDX.Direct2D1.Bitmap.FromWicBitmap 加载 Tiff CCITTv4 时出现 BadImage 错误
【发布时间】:2015-05-26 13:37:44
【问题描述】:

我正在使用 SharpDX 及其随附的 WIC 和 Direct2D 包装器进行一些服务器端图像处理。

以下代码非常适用于 JPEG 图像,并以 SharpDX 文档和 this Microsoft sample using D2D directly via C++ 为模型。

但是,当我尝试加载 TIFF CCITT(双色 1bpp)图像时出现 BadImage 错误。 BadImage 错误仅在EndDraw (稍后在注释的DrawEndorsement 函数中发生)或我插入的这行代码中引发问题发生更明显:

SharpDX.Direct2D1.Bitmap bitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(_renderTarget, _wicBitmap);

我传入的 JPEG 图像到达这一点并继续没有问题,但我传入的 TIFF 到达这一点并导致 FromWicBitmap 因 BadImage 错误而出错。

我正在使用FormatConverter 将 TIFF/JPEG 像素格式转换为适当且受支持的 D2D 像素格式,并且转换器确实更改了两个图像的像素格式 GUID,但同样,FromWicBitmap barfs 仅在TIFF。

我以为我在转换或滥用 SharpDX/D2D 时做错了,但是当我构建并运行上述 Microsoft C++ D2D 图像查看器示例时,它加载并渲染了这个 same TIFF 文件,但没有错误。我仔细检查了示例的代码,以确认我使用了所有相同的像素格式、选项等,看起来我对 SharpDX 所做的事情与示例直接对 D2D 所做的事情几乎完全相同。

很明显 Direct2D 不喜欢 WIC 处理的 TIFF 图像的像素格式,但为什么 MS 示例没有表现出相同的行为,为什么 FormatConverter 没有修复它?

  1. 我是否遗漏了 D2D 示例代码正在执行的操作?
  2. 我错过了 SharpDX 的一些技巧吗?
  3. 这是 SharpDX 错误吗?

谢谢!

public byte[] BuildImage(byte[] image, Format saveFormat)
{
    SharpDX.WIC.Bitmap _wicBitmap;
    WicRenderTarget _renderTarget;
    BitmapFrameDecode bSource;
    FormatConverter converter = new FormatConverter(_factoryManager.WicFactory);


    using (MemoryStream systemStream = new MemoryStream(image))
        using (WICStream wicStream = new WICStream(_factoryManager.WicFactory, systemStream))
        {
            BitmapDecoder inDecoder = new BitmapDecoder(_factoryManager.WicFactory, wicStream, DecodeOptions.CacheOnLoad);
            if (inDecoder.FrameCount > 0)
            {
                bSource = inDecoder.GetFrame(0);
                converter.Initialize(bSource, SharpDX.WIC.PixelFormat.Format32bppPRGBA, BitmapDitherType.Solid, null, 0.0f, BitmapPaletteType.MedianCut);
                _imageWidth = bSource.Size.Width;
                _imageHeight = bSource.Size.Height;

            }
            else
            {
                throw new Exception("No frames found!");
            }
        }

    _wicBitmap = new SharpDX.WIC.Bitmap(
            _factoryManager.WicFactory,
            converter,
            BitmapCreateCacheOption.CacheOnDemand
            );

    _renderTarget = new WicRenderTarget(_factoryManager.D2DFactory, _wicBitmap, new RenderTargetProperties());

    SharpDX.Direct2D1.Bitmap bitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(_renderTarget, _wicBitmap);

    //DrawEndorsement(_renderTarget);

    _renderTarget.Dispose();
    bSource.Dispose();
    converter.Dispose();

    return SaveImage(saveFormat, _wicBitmap);
}

【问题讨论】:

  • 您可以尝试同时删除两者吗? (根本不处理 WicStream)公平地说,我怀疑转换器要求流仍处于打开状态。
  • 当我注意到 JPEG 图像损坏时,我实际上在测试中发现了这一点。事实证明,它还表现为 TIFF 的 Badimage 错误。德普。谢谢!

标签: c# image tiff direct2d sharpdx


【解决方案1】:

正如 xoofx 指出的那样,事实证明这是由于我在 FormatConverter 仍在使用时处置了其底层的 WIC/MemoryStreams。

这导致 JPEG 在写入时损坏,并且奇怪地导致 TIFF 在此之前就失败了。

相应地扩展了使用范围并修复了它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-20
    • 2011-05-20
    • 2016-09-11
    • 1970-01-01
    • 2019-11-07
    • 2020-09-27
    • 1970-01-01
    • 2014-04-02
    相关资源
    最近更新 更多