示例#1
0
        public static void MakeThumbnail(string path, ref string outputPath)
        {
            if (!File.Exists(Properties.Settings.Default.UtilitiesDirectory + "gdal_translate.exe"))
            {
                MessageBox.Show("В папке с утилитами отсутствует утилита gdal_translate.", "Ошибка!",
                                MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            try
            {
                /* -------------------------------------------------------------------- */
                /*      Register driver(s).                                             */
                /* -------------------------------------------------------------------- */
                GdalConfiguration.ConfigureGdal();
                //Gdal.AllRegister();

                /* -------------------------------------------------------------------- */
                /*      Open dataset.                                                   */
                /* -------------------------------------------------------------------- */
                Dataset ds = Gdal.Open(path, Access.GA_ReadOnly);

                if (ds == null)
                {
                    Console.WriteLine("Can't open " + path);
                    return;
                    //System.Environment.Exit(-1);
                }

                Console.WriteLine("Raster dataset parameters:");
                Console.WriteLine("  Projection: " + ds.GetProjectionRef());
                Console.WriteLine("  RasterCount: " + ds.RasterCount);
                Console.WriteLine("  RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")");

                /* -------------------------------------------------------------------- */
                /*      Get driver                                                      */
                /* -------------------------------------------------------------------- */
                Driver drv = ds.GetDriver();

                if (drv == null)
                {
                    Console.WriteLine("Can't get driver.");
                    return;
                    //System.Environment.Exit(-1);
                }

                Console.WriteLine("Using driver " + drv.LongName);

                string m_Gdal_Translate_Arguments = String.Empty;

                Band band = ds.GetRasterBand(1);
                if (band.DataType == DataType.GDT_Byte || drv == Gdal.GetDriverByName("JPEG"))
                {
                    m_Gdal_Translate_Arguments = "-of JPEG -co \"QUALITY=50\" ";
                    outputPath = Path.ChangeExtension(outputPath, "jpg");
                }
                else
                {
                    m_Gdal_Translate_Arguments = "-of GTiff -co \"COMPRESS = DEFLATE\" -co \"ZLEVEL = 9\" ";
                    outputPath = Path.ChangeExtension(outputPath, "tif");
                }

                // Get the width and height of the Dataset
                int width  = band.XSize;
                int height = band.YSize;

                float scaleCoeff = 1;
                float maxSize    = 256;
                scaleCoeff = width > height ? maxSize / (float)width : maxSize / (float)height;
                int newWidth  = (int)((float)width * scaleCoeff);
                int newHeight = (int)((float)height * scaleCoeff);

                m_Gdal_Translate_Arguments +=
                    String.Format("-outsize {0} {1} \"{2}\" \"{3}\"", newWidth, newHeight, path, outputPath);

                //UseGdalTranslate(path, outputPath, newWidth, newHeight);
                UseGdalTranslate(m_Gdal_Translate_Arguments);
            }
            catch (Exception e)
            {
                Console.WriteLine("Application error: " + e.Message);
                outputPath = String.Empty;
            }
        }