【问题标题】:Displaying large image with SharpDX使用 SharpDX 显示大图像
【发布时间】:2017-05-31 19:52:51
【问题描述】:

我正在尝试使用 SharpDX 从文件中绘制大图像 (21000x7000)。

代码如下所示:

        if (newBitmap==null)
        {
            ImagingFactory imagingFactory = new ImagingFactory();
            NativeFileStream fileStream = new NativeFileStream(@"D:\path\myfile.png",
                NativeFileMode.Open, NativeFileAccess.Read);

            BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream, DecodeOptions.CacheOnDemand);
            BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

            FormatConverter converter = new FormatConverter(imagingFactory);
            //                                                                      Format32bppPArgb
            //                                                                      Format32bppPRGBA
            converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);

            newBitmap = SharpDX.Direct2D1.Bitmap1.FromWicBitmap(target, converter); 
        }
        target.DrawBitmap(newBitmap,new RawRectangleF(0,0,target.Size.Width,target.Size.Height),1,BitmapInterpolationMode.Linear );

执行时,FromWicBitmap 函数出现以下异常:

Exception thrown: 'SharpDX.SharpDXException' in SharpDX.dll

Additional information: HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: The parameter is incorrect.

当我将图像大小调整为 2100x700 时,它可以正常加载和显示,所以问题在于图像大小。

我想要这个尺寸的图像,因为以后我打算添加缩放功能。

我对 Direct2D 完全陌生。处理这个问题的最佳方法是什么?

【问题讨论】:

标签: c# image direct2d sharpdx wic


【解决方案1】:

根据您的描述,听起来问题超出了 Direct3D 的纹理大小限制(Direct2D 使用 Direct3D 进行工作,因此具有相同的约束)。

如果您查看 Direct3D Feature Level table 中的 Max Texture Dimension,您会看到范围从 4,09616,384 像素,具体取决于图形硬件级别。你可以:

  • 假设 4,096 是您的极限(如果您在具有不同显卡的各种计算机上运行此程序,则应该这样做)
  • 运行 dxdiag.exe,单击“显示 1”选项卡,然后查找 Feature LevelsDDI 以查看可用的功能级别。

解决方案是在调用target.DrawBitmap 之前缩小图像大小,直到它符合像素限制。换句话说,你不能像你希望的那样轻松地进行缩放——即使你放大和缩小纹理,在某些时候你必须重新采样,否则它会比原始图像更像素化。

【讨论】:

  • 或者在这种情况下,您可以将大图像拆分为 4k 像素图像块并单独绘制。我猜这是 OP 要求的。
  • @SharpShade 在线查看 API 后,RenderTarget.DrawBitmap 似乎不支持定位信息,因此切割位图只会将一个部分呈现在另一个之上。没有一种简单的方法可以做到这一点,并且重写代码以呈现比简单地调整图像大小更复杂的东西。我将更新我的回复以引用缩放。
  • 好吧,我没有使用过 Direct2D,但使用该方法绘制纹理似乎很奇怪,不是吗?然后我想唯一的解决方法是用不同的方式绘制它并通过矩阵进行转换。但无论哪种方式都可能 - 尽管下采样会导致分辨率较低的缩放。
猜你喜欢
  • 1970-01-01
  • 2020-10-26
  • 2012-06-23
  • 2014-01-18
  • 1970-01-01
  • 2015-12-05
  • 2018-02-04
  • 2013-08-21
  • 1970-01-01
相关资源
最近更新 更多