Search code examples
c#imagedirect2dsharpdxwic

Displaying large image with SharpDX


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?


Solution

  • 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.