Exception while using GDAL in C#

痞子三分冷 提交于 2019-12-04 00:17:15

问题


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

This is the full error message:

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


回答1:


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.




回答2:


To solve this one I downloaded the prebuilt libraries as described here and grabbed FWTools from here.

The unmanaged DLLs I used came from \install_dir\FWTools2.4.7\bin and the C# wrapper from \install_dir\FWTools2.4.7\csharp.

gdal14.dll, msvcp71.dll and msvcr71.dll came from here, which is mentioned in that first link.

The error you are receiving re gdal_wrap.dll is referring to one of its dependencies. I threw that DLL into depends and it found a lengthy list of dependent libraries. Note that this list is likely longer due to my use of the FWTools distribution - if you built your version from source it may look different, though the same principles apply.

To get the above code to work on my machine I had the following files in my output directory:

gdal14.dll
gdalconst_csharp.dll
gdalconst_wrap.dll
gdal_csharp.dll
gdal_fw.dll
gdal_wrap.dll
geos_fw.dll
geotiff_fw.dll
hdf5dll.dll
hdf_fw.dll
jpeg12_osgeo.dll
jpeg_osgeo.dll
libcurl.dll
libeay32.dll
libexpat.dll
libmysql.dll
libpq.dll
libtiff_fw.dll
lti_dsdk_dll.dll
mfhdf_fw.dll
msvcp71.dll
msvcr71.dll
NCScnet_fw.dll
NCSEcw_fw.dll
NCSUtil_fw.dll
netcdf.dll
ogdi_32b1.dll
proj.dll
sqlite3.dll
ssleay32.dll
szlibdll.dll
xerces-c_2_7.dll
zlib1.dll
zlib_osgeo.dll

Now these don't necessarily all have to live in the output directory - as long as they are on your path somewhere (e.g., \Windows\System32) you should be fine.




回答3:


I know it's an old question, but I believe my answer might help someone.

I was able to successfully compile and run examples using c# gdal by doing the following:

  1. Downloading GDAL sdk from http://www.gisinternals.com/ (64 bit in my case)
  2. Executing the SDKShell.bat script to set the system environment paths, etc.
  3. Creating a project in Visual Studio. And referencing all .net dlls (the ones that names end with _csharp.dll), located in \bin\gdal\csharp\ inside downloaded SDK
  4. Setting platform target in Visual Studio project settings to x64 to get rid og bad image format exceptions. The last step wouldn't be necessary if I'd choosse 32bit version of SDK to work with.

I did not install fwtools at all. It seems like the last build of fw_tools is relatively old, and sdk is still maintained.




回答4:


I know this is a reasonably old question now, but I found this in google after researching the same problem myself, so this means that for searches on this error this is still a very relevant page to update given it's still in the top 5 from the big G when the same problem is searched.

In my case it was the answers from "DeusExMachina25" and "Grzegorz Sławecki" that struck a chord.

I'm writing some software that makes use of the current builds of "sharp map" on NUGet (as of 24th of June 2016) and my software kept throwing the same gdal_wrap message as the OP originally reported, even though I'm using the GDAL package provided by the Sharpmap team.

I didn't realize that the NUGet installer for the package had installed a configuration class for me, but after reading through this thread and finding out that it does I went looking for it.

Sure enough I found the file 'GdalConfiguration.cs' in my project and added a call to it in an appropriate place in my project, expecting GDAL to be initialized correctly.

However, after I did this, I still had the same problem.

So, I set a break point on the beginning of the GDAL routine that had been added, and waited until the break point was hit.

I then traced through the method, and eventually found the following line:

var gdalPath = Path.Combine(executingDirectory, "gdal");

at around line 64 in the file.

Tracing through this, I noticed that the path being built was:

d:\geodata\maptest\maptest\bin\debug\gdal

but the NUGet installer had installed all the dependent assemblies in

d:\geodata\maptest\maptest\bin\debug

Exactly where I expected them to be.

I changed line 64 so that it now read:

var gdalPath = Path.Combine(executingDirectory, "");

and voila, the error went away and everything started to work.

I could have done things the other way too, and created a folder called gdal, then copied everything into that, but that then would have gotten deleted when I did a "clean" on the project.

Since the config class, set's up various environment variables based on this path, quickly changing that one line also fixes up the path for the GDAL data files , plugins and a few other things too.




回答5:


You could try using Dependency Walker to see if there are any dlls that gdal_csharp is trying to grab but cannot.




回答6:


Have you added the path to your GDAL libraries to your PATH environment variable? I downloaded my files from http://vbkto.dyndns.org/sdk/?_sm_au_=iVVqjsHS2n46WP00 and here's my path: C:\libs\release-1600-gdal-1-9-mapserver-6-2\bin.




回答7:


To use the C#-bindings of GDAL you need an installation of FWTools (from http://fwtools.maptools.org/) as well as the most current binaries that match your system (from http://vbkto.dyndns.org/sdk/). Afterwards it is important to include the bin-directory of FWTools (example for 64bit-systems: C:\Program Files (x86)\FWTools2.4.7\bin) in your PATH variable as well as the necessary dlls (gdal_csharp.dll was mentioned in the question) in your Visual Studio project references. I outlined the complete processs here.

This process works on 32 bit as well as 64 bit systems, I tested it with VS 2010 and 2012.




回答8:


Remove the path to python from the system variables. Because the main gdal paths conflict with python 27



来源:https://stackoverflow.com/questions/14353502/exception-while-using-gdal-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!