Exemple #1
0
        /// <summary>
        /// Load media from stream.
        /// </summary>
        /// <param name="stream">
        /// A <see cref="Stream"/> where the media data is stored.
        /// </param>
        /// <param name="criteria">
        /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an media stream.
        /// </param>
        /// <returns>
        /// An <see cref="Image"/> holding the media data.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <paramref name="stream"/> or <paramref name="criteria"/> is null.
        /// </exception>
        public Image Load(Stream stream, ImageCodecCriteria criteria)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (criteria == null)
            {
                throw new ArgumentNullException("criteria");
            }

            const int BufferBlockSize = 4096;

            byte[] buffer = new byte[stream.Length];
            int    bufferOffset = 0, bytesRed;

            // Read the stream content
            while ((bytesRed = stream.Read(buffer, bufferOffset, BufferBlockSize)) > 0)
            {
                bufferOffset += bytesRed;
            }

            GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            try {
                Gdal.FileFromMemBuffer("/vsimem/GdalLoad", buffer.Length, bufferHandle.AddrOfPinnedObject());
                using (Dataset dataset = Gdal.Open("/vsimem/GdalLoad", Access.GA_ReadOnly)) {
                    return(Load(dataset, criteria));
                }
            } finally {
                // Release virtual path
                Gdal.Unlink("/vsimem/GdalLoad");
                bufferHandle.Free();
            }
        }
Exemple #2
0
        public static Dataset ToMemDataset(this Image image, string memPath = "/vsimem/inmemfile")
        {
            Dataset dataset = null;

            if (image == null)
            {
                throw new Exception("image不能为空");
            }
            if (memPath?.StartsWith("/vsimem/") != true)
            {
                throw new Exception("memPath必须以“/vsimem/”开头");
            }
            byte[] buffer = null;
            using (MemoryStream ms = new MemoryStream())
            {
                var format = image.RawFormat.ToString();
                if (format.Contains("[ImageFormat:"))
                {
                    throw new Exception("image格式不正确");
                }
                image.Save(ms, image.RawFormat);
                buffer = ms.GetBuffer();
            }
            Gdal.FileFromMemBuffer(memPath, buffer);
            dataset = Gdal.Open(memPath, Access.GA_ReadOnly);
            return(dataset);
        }
Exemple #3
0
    public static void Main(string[] args)
    {
        if (args.Length != 1)
        {
            usage();
        }

        byte[] imageBuffer;

        using (FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Read))
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                long numBytes = new FileInfo(args[0]).Length;
                imageBuffer = br.ReadBytes((int)numBytes);
                br.Close();
                fs.Close();
            }
        }

        Gdal.AllRegister();

        string memFilename = "/vsimem/inmemfile";

        try
        {
            Gdal.FileFromMemBuffer(memFilename, imageBuffer);
            Dataset ds = Gdal.Open(memFilename, Access.GA_ReadOnly);

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

            Driver drv = Gdal.GetDriverByName("GTiff");

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

            drv.CreateCopy("sample.tif", ds, 0, null, null, null);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            Gdal.Unlink(memFilename);
        }
    }