using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "栅格数据(*.jpg,*.bmp,*.tiff,*.tif)|*.jpg;*.bmp;*.tiff;*.tif";
if (ofd.ShowDialog() == DialogResult.OK)
{
string fullFilePath = ofd.FileName;
int index = fullFilePath.LastIndexOf("\\");
//获得文件路径
string filePath = fullFilePath.Substring(0,index);
//获得文件名
string fileName = fullFilePath.Substring(index+1);
IWorkspaceFactory workspcFac = new RasterWorkspaceFactory();
IRasterWorkspace rasterWorkspc;
IRasterDataset rasterDatst = new RasterDatasetClass();
IRasterLayer rasterLay = new RasterLayerClass();
rasterWorkspc = workspcFac.OpenFromFile(filePath, 0) as IRasterWorkspace;
rasterDatst = rasterWorkspc.OpenRasterDataset(fileName);
rasterLay.CreateFromDataset(rasterDatst);
mapControl.AddLayer(rasterLay);
mapControl.Refresh();
}
}