Пример #1
0
        /// <summary>
        /// Adds an image to the repository with the specified image processing options.
        /// </summary>
        /// <param name="stream">The stream source containing the image.</param>
        /// <param name="options">The image processing options.</param>
        /// <returns>The image key to access the image.</returns>
        public ImageKey Add(Stream stream, ImageOptions options)
        {
            var id     = SecureGuid.Create();
            var format = Add(id, null, stream, options);

            return(new ImageKey(id, format));
        }
Пример #2
0
        /// <summary>
        /// Adds an image to the repository in its original format without applying any editors. The image must be in one of the supported formats (see <see
        /// cref="ImageFormat"/>).
        /// </summary>
        /// <param name="stream">The stream source containing the image.</param>
        /// <param name="validateSource">
        /// Optional function used to validate the source image. Only headers are loaded in the image passed into the function.
        /// </param>
        /// <returns>The image key to access the image.</returns>
        public ImageKey Add(Stream stream, Action <Image>?validateSource)
        {
            if (!stream.CanSeek)
            {
                var oldStream = stream;
                stream = new MemoryStream();
                oldStream.CopyTo(stream);
            }

            long        startPosition = stream.Position;
            ImageFormat format;

            using (var image = Image.FromStream(stream, false, false)) {
                validateSource?.Invoke(image);

                if (image.RawFormat.Guid == SystemImageFormat.Jpeg.Guid)
                {
                    format = ImageFormat.Jpeg;
                }
                else
                {
                    throw new ArgumentException("The stream does not have a valid image format.", nameof(stream));
                }
            }

            stream.Position = startPosition;

            var id       = SecureGuid.Create();
            var imageKey = new ImageKey(id, format);
            var filePath = GetAbsoluteImagePath(imageKey);

            using var fs = filePath.OpenStream(FileMode.CreateNew);
            stream.CopyTo(fs);

            return(imageKey);
        }