出现这个的原因大概是:
1、32位和64位用错,32位程序用的64位的dll,64位程序使用32位的dll。
2、缺少依赖库。
3、环境变量配置错误。
缺少依赖库
首先找到缺少哪些关联库,也可以直接把bin目录下的所有dll拷贝过去
查询gdal***.dll的关联动态库:
1、找到vs的命令提示符,如图
2、输入:dumpbin /dependents c:\…\…\…\gdal***.dll即可看到该dll所关联的dll
3、将gadl中能查到的属于上图中关联的dll,和gdal***.dll拷贝至项目的/bin/debug。最终还需要放在项目中的dll大致是这些:
环境变量配置错误
解决方法:采用SharpMap的GDAL初始化方法解决,需要两个数据:
1. GdalConfiguration.cs
2. gdal_data_config.rar
注:以上两文件下载地址:gdal_csharp开发环境配置文件
第一步:将GdalConfiguration.cs添加到项目中,然后解压gdal_data_config.rar到debug目录下,文件夹名称为gdal。
第二步:在使用Gdal.AllRegister()初始化前,调用以下两句代码进行相关初始化数据的配置即可。
SharpMap.GdalConfiguration.ConfigureGdal();
SharpMap.GdalConfiguration.ConfigureOgr();
GdalConfiguration类的内容如下:
/******************************************************************************
*
* Name: GdalConfiguration.cs.pp
* Project: GDAL CSharp Interface
* Purpose: A static configuration utility class to enable GDAL/OGR.
* Author: Felix Obermaier
*
*****************************************************************************/
using System;
using System.IO;
using System.Reflection;
using Gdal = OSGeo.GDAL.Gdal;
using Ogr = OSGeo.OGR.Ogr;
namespace SharpMap
{
public static partial class GdalConfiguration
{
private static bool _configuredOgr;
private static bool _configuredGdal;
/// <summary>
/// Function to determine which platform we're on
/// </summary>
private static string GetPlatform()
{
return IntPtr.Size == 4 ? "x86" : "x64";
}
/// <summary>
/// Construction of Gdal/Ogr
/// </summary>
static GdalConfiguration()
{
var executingAssemblyFile = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
var executingDirectory = Path.GetDirectoryName(executingAssemblyFile);
if (string.IsNullOrEmpty(executingDirectory))
throw new InvalidOperationException("cannot get executing directory");
var gdalPath = Path.Combine(executingDirectory, "gdal");
var nativePath = Path.Combine(gdalPath, GetPlatform());
// Prepend native path to environment path, to ensure the
// right libs are being used.
var path = Environment.GetEnvironmentVariable("PATH");
path = nativePath + ";" + Path.Combine(nativePath, "plugins") + ";" + path;
Environment.SetEnvironmentVariable("PATH", path);
// Set the additional GDAL environment variables.
var gdalData = Path.Combine(gdalPath, "data");
Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
Gdal.SetConfigOption("GDAL_DATA", gdalData);
var driverPath = Path.Combine(nativePath, "plugins");
Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", driverPath);
Gdal.SetConfigOption("GDAL_DRIVER_PATH", driverPath);
Environment.SetEnvironmentVariable("GEOTIFF_CSV", gdalData);
Gdal.SetConfigOption("GEOTIFF_CSV", gdalData);
var projSharePath = Path.Combine(gdalPath, "share");
Environment.SetEnvironmentVariable("PROJ_LIB", projSharePath);
Gdal.SetConfigOption("PROJ_LIB", projSharePath);
}
/// <summary>
/// Method to ensure the static constructor is being called.
/// </summary>
/// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
public static void ConfigureOgr()
{
if (_configuredOgr) return;
// Register drivers
Ogr.RegisterAll();
_configuredOgr = true;
}
/// <summary>
/// Method to ensure the static constructor is being called.
/// </summary>
/// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
public static void ConfigureGdal()
{
if (_configuredGdal) return;
// Register drivers
Gdal.AllRegister();
_configuredGdal = true;
}
}
}