Exception while using GDAL in C#

Hossein Narimani Rad picture Hossein Narimani Rad · Jan 16, 2013 · Viewed 14.8k times · Source

I started to use gdal_csharp dll in my application and read a geotiff file. but it says:

The type initializer for 'OSGeo.GDAL.GdalPINVOKE' threw an exception.

it's my code

string fileName = @"/path to geotiff file";

OSGeo.GDAL.Dataset DS = 
    OSGeo.GDAL.Gdal.Open(fileName, OSGeo.GDAL.Access.GA_ReadOnly);

can anyone help?

Edit:

I have these dlls

enter image description here

This is the full error message:

enter image description here

It says that cannot load gdal_wrap. But when I'm going to add that dll to my application the below message is shown:

enter image description here

Answer

DeusExMachina25 picture DeusExMachina25 · Jun 1, 2016

As an update to this there is now GDAL maintained by the SharpMap team as a nuget package here which is updated regularly. You'll need to install both the "GDAL.Native" and "GDAL" package for your project to use the GDAL library. Once installed via nuget, they'll automatically create a "GdalConfiguration.cs" that you call into to initialize the GDAL paths before starting. The only thing to note is the packages are setup to automatically copy their appropriate GDAL libraries to your output build directory. If you need to deploy the application you'll have to do a bit of extra effort.