public async Task <ProcessImageResult> ProcessImageAsync(ProcessImageQuery query, bool disposeOutput = true)
        {
            Guard.NotNull(query, nameof(query));

            ValidateQuery(query);

            var  watch = new Stopwatch();
            long len;
            IProcessableImage image = null;

            try
            {
                watch.Start();

                var source = query.Source;

                // Load source
                if (source is byte[] b)
                {
                    using var memStream = new MemoryStream(b);
                    image = await Factory.LoadAsync(memStream);

                    len = b.LongLength;
                }
                else if (source is Stream s)
                {
                    image = await Factory.LoadAsync(s);

                    len = s.Length;
                }
                else if (source is string str)
                {
                    str   = NormalizePath(str);
                    image = Factory.Load(str);
                    len   = (new FileInfo(str)).Length;
                }
                else if (source is IFile file)
                {
                    using (var fs = file.OpenRead())
                    {
                        image = await Factory.LoadAsync(fs);

                        len = file.Length;
                    }
                }
                else
                {
                    throw new ProcessImageException("Invalid source type '{0}' in query.".FormatInvariant(query.Source.GetType().FullName), query);
                }

                var sourceFormat = image.Format;

                // Pre-process event
                await _eventPublisher.PublishAsync(new ImageProcessingEvent(query, image));

                var result = new ProcessImageResult
                {
                    Query        = query,
                    SourceFormat = image.Format,
                    Image        = image,
                    DisposeImage = disposeOutput
                };

                // >>>>>> Core processing
                ProcessImageCore(query, image, out var fxApplied);

                result.HasAppliedVisualEffects = fxApplied;

                // Post-process event
                await _eventPublisher.PublishAsync(new ImageProcessedEvent(query, result));

                result.ProcessTimeMs = watch.ElapsedMilliseconds;

                return(result);
            }
            catch (Exception ex)
            {
                throw new ProcessImageException(query, ex);
            }
            finally
            {
                if (query.DisposeSource && query.Source is IDisposable source)
                {
                    source.Dispose();
                }

                watch.Stop();
                _totalProcessingTime += watch.ElapsedMilliseconds;
            }
        }
Beispiel #2
0
 public ImageProcessedEvent(ProcessImageQuery query, ProcessImageResult result)
 {
     Query  = query;
     Result = result;
 }