コード例 #1
0
        /// <summary>
        /// Adds an image size to the repository. An <see cref="IOException"/> will be thrown if the size already exists or if multiple threads/processes
        /// attempt to add the same size to the same image simultaneously.
        /// </summary>
        /// <param name="imageKey">The image key of the image to use as a source for the new size.</param>
        /// <param name="sizeId">A string that is appended to the file name to identify the size.</param>
        /// <param name="options">The image options for the new size.</param>
        /// <returns>
        /// The image key for the new image size, which has the same ID as the source image and a format determined by <paramref name="options"/>.
        /// </returns>
        public ImageKey AddSize(ImageKey imageKey, string sizeId, ImageOptions options)
        {
            sizeId = sizeId.Trim();

            if (sizeId.Length == 0)
            {
                throw new ArgumentException("A size identifier is required.", nameof(sizeId));
            }

            if (options.ImageEditor == null)
            {
                throw new ArgumentException("No image editor specified in options.", nameof(options));
            }

            using var stream = GetAbsoluteImagePath(imageKey, null).OpenStream(access: FileAccess.Read, share: FileShare.Read | FileShare.Delete);
            var format = Add(imageKey.Id, sizeId, stream, options);

            return(new ImageKey(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);
        }
コード例 #3
0
 /// <summary>
 /// Gets the path to an image relative to the base directory of this image host. This method does not validate that the file exists.
 /// </summary>
 /// <param name="imageKey">The image key.</param>
 /// <param name="sizeId">The size identifier string, or <see langword="null"/> to get the main image.</param>
 public IRelativeFilePath GetRelativeImagePath(ImageKey imageKey, string?sizeId = null)
 {
     return(GetRelativeFilePath(imageKey.Id, imageKey.Format.GetExtension(), sizeId));
 }
コード例 #4
0
 /// <summary>
 /// Gets the absolute path to an image. This method does not validate that the file exists.
 /// </summary>
 /// <param name="imageKey">The image key.</param>
 /// <param name="sizeId">The size identifier string, or <see langword="null"/> to get the main image.</param>
 public IAbsoluteFilePath GetAbsoluteImagePath(ImageKey imageKey, string?sizeId = null)
 {
     return(GetAbsoluteFilePath(imageKey.Id, imageKey.Format.GetExtension(), sizeId));
 }