使用SharpDX显示大图像 [英] Displaying large image with SharpDX

查看:667
本文介绍了使用SharpDX显示大图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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 function:

 抛出异常:SharpDX.dll中的'SharpDX.SharpDXException'

附加信息:HRESULT:[0x80070057],模块:[General],ApiCode:[E_INVALIDARG / Invalid Arguments],消息:参数不正确。

当我将图像尺寸调整为2100x700时,它会加载并显示得很好,所以问题在于图像尺寸。



我想要这个尺寸的图像,因为后来我计划添加缩放功能。



我是Direct2D的新手。解决这个问题的最佳方法是什么?

解决方案

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



如果你在 Max Texture Dimension https://msdn.microsoft.com/en-us/library/windows/desktop/ff476876(v=vs.85).aspx#Overview =nofollow noreferrer> Direct3D功能级别表你会看到范围从 4,096 16,384像素,具体取决于图形硬件的级别。您可以:




  • 假设4,096是您的限制(如果您在各种计算机上运行此程序,则应该执行此操作不同的图形卡)

  • 运行dxdiag.exe,单击显示1选项卡,然后查找功能级别 DDI 查看可用的功能级别。



解决方案是调整图像大小,直到它符合像素限制,然后再调用 target.DrawBitmap 。换句话说,您不能像想象的那样轻松地进行缩放工作 - 即使您放大和缩小纹理,在某些时候您将不得不重新采样,或者它将比原始图像更像素化。 / p>

I'm trying to use SharpDX to draw a large image (21000x7000) from file.

The code looks like this:

        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 );

When executing, I'm getting following exception in the FromWicBitmap function:

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

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

When I resize the image to 2100x700, it loads and displays just fine, so the problem is with image size.

I want the image of this size because later I'm planning on adding zooming functionality.

I'm totally new to Direct2D. What is the best way to deal with this?

解决方案

From your description, it sounds like the issue is exceeding Direct3D's texture size limits (Direct2D uses Direct3D for its work so it has the same constraints).

If you look at Max Texture Dimension in the Direct3D Feature Level table you'll see that ranges from 4,096 to 16,384 pixels depending on the level of graphics hardware. You can:

  • assume 4,096 is your limit (this is what you should do if you are running this program on a variety of computers that have different graphics cards)
  • run dxdiag.exe, clicking the "Display 1" tab, and looking for Feature Levels or DDI to see what feature levels are available.

The solution is to resize your image down until it fits within the pixel limits before calling target.DrawBitmap. In other words, you can't make zooming work as easily as you might like - even if you zoom in and out on a texture, at some point you'll have to resample or it will be more pixelated than the original image.

这篇关于使用SharpDX显示大图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆