private void ApplyResize(SKBitmap original, InputImageParameters inputImage, OutputImageParameters outputImage)
        {
            if (!outputImage.Resized)
            {
                return;
            }

            using (SKBitmap resized = original.Resize(new SKImageInfo(outputImage.Width, outputImage.Height), SKFilterQuality.High))
            {
                if (resized == null)
                {
                    throw new ArgumentException {
                              Data = { { "ValidationData", new { Body = new { Image = "Something wrong with incoming image" } } } }
                    }
                }
                ;

                using (SKImage image = SKImage.FromBitmap(resized))
                {
                    MemoryStream output = new MemoryStream();
                    image.Encode(inputImage.Format, outputImage.Quality).SaveTo(output);

                    outputImage.OutputStream = output;
                }
            }
        }
        private OutputImageParameters Resize(SKBitmap original, InputImageParameters inputImage, ImageSizeParam targetSize)
        {
            OutputImageParameters outputImage = CalculateSize(original.Width, original.Height, targetSize, inputImage.MinimumDifference);

            ApplyResize(original, inputImage, outputImage);

            return(outputImage);
        }
Ejemplo n.º 3
0
        public async Task UploadImage(string templateUrl, string contentType, OutputImageParameters outputImageParameters)
        {
            string url = _templateParametersService.ReplaceParameters(
                templateUrl,
                ImageUrlTemplateFactory.GetUrlParameters(outputImageParameters.Width.ToString(), outputImageParameters.Height.ToString(), outputImageParameters.Quality.ToString()),
                ImageUrlTemplateFactory.FormatParameter
                );

            using (HttpResponseMessage response = await _httpClient.PutAsync(url, new StreamContent(outputImageParameters.OutputStream)
            {
                Headers = { { "Content-Type", contentType } }
            }))
            {
                if (!response.IsSuccessStatusCode)
                {
                    throw new HttpRequestException($"Image upload PUT {url} failed with response: \r\n{await response.Content.ReadAsStringAsync()}");
                }
            }
        }