private async Task SaveToFile(string fileName) { var fileExtension = fileName.Split('.').ToArray().Last(); var shapefile = fileExtension == "shp"; string fileNameGTiff = null; fileNameGTiff = shapefile ? $"{fileName}.tmp" : fileName; await Task.Run(() => { string[] options = { "PHOTOMETRIC=RGB", "PROFILE=GeoTIFF" }; var finalDataset = Gdal.GetDriverByName("GTiff").Create(fileNameGTiff, _processingBand.XSize, _processingBand.YSize, 3, DataType.GDT_Byte, options); finalDataset.SetGeoTransform(_geoTransform); var spatialReference = new SpatialReference(WKT); spatialReference.ExportToWkt(out var exportedWkt); finalDataset.SetProjection(exportedWkt); var redBand = GetByteArrayFromRaster(_processedImage, _processingBand.XSize, _processingBand.YSize, 0); var greenBand = GetByteArrayFromRaster(_processedImage, _processingBand.XSize, _processingBand.YSize, 1); var blueBand = GetByteArrayFromRaster(_processedImage, _processingBand.XSize, _processingBand.YSize, 2); finalDataset.GetRasterBand(1).WriteRaster(0, 0, _processingBand.XSize, _processingBand.YSize, redBand, _processingBand.XSize, _processingBand.YSize, 0, 0); finalDataset.GetRasterBand(2).WriteRaster(0, 0, _processingBand.XSize, _processingBand.YSize, greenBand, _processingBand.XSize, _processingBand.YSize, 0, 0); finalDataset.GetRasterBand(3).WriteRaster(0, 0, _processingBand.XSize, _processingBand.YSize, blueBand, _processingBand.XSize, _processingBand.YSize, 0, 0); if (shapefile) { string[] optionsShapefile = null; var finalDatasource = Ogr.GetDriverByName("ESRI Shapefile") .CreateDataSource(fileName, optionsShapefile); var layer = finalDatasource.CreateLayer("trees", spatialReference, wkbGeometryType.wkbPolygon, optionsShapefile); Gdal.Polygonize(finalDataset.GetRasterBand(1), finalDataset.GetRasterBand(1), layer, -1, null, null, null); finalDatasource.FlushCache(); } else { finalDataset.FlushCache(); } }); }