Example #1
0
        /// <summary>
        /// Parse request details from relative path
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private ThumbnailRequest ParseRequest(HttpRequest request)
        {
            var req = new ThumbnailRequest();

            req.RequestedPath      = request.Path;
            req.ThumbnailSize      = ParseSize(request.Query["size"]);
            req.SourceImagePath    = GetPhysicalPath(request.Path);
            req.ThumbnailImagePath = GenerateThumbnailFilePath(request.Path, req.ThumbnailSize);

            return(req);
        }
Example #2
0
        /// <summary>
        /// Generates thumbnail image, cache and write to output stream
        /// </summary>
        /// <param name="request"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        private async Task GenerateThumbnail(ThumbnailRequest request, Stream stream)
        {
            if (File.Exists(request.SourceImagePath))
            {
                Image image = Image.FromFile(request.SourceImagePath);

                System.Drawing.Image thumbnail =
                    new Bitmap(request.ThumbnailSize.Value.Width, request.ThumbnailSize.Value.Height);
                System.Drawing.Graphics graphic =
                    System.Drawing.Graphics.FromImage(thumbnail);

                graphic.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                graphic.SmoothingMode      = SmoothingMode.HighQuality;
                graphic.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                graphic.CompositingQuality = CompositingQuality.HighQuality;

                double ratioX = (double)request.ThumbnailSize.Value.Width / (double)image.Width;
                double ratioY = (double)request.ThumbnailSize.Value.Height / (double)image.Height;
                double ratio  = ratioX < ratioY ? ratioX : ratioY;

                int newHeight = Convert.ToInt32(image.Height * ratio);
                int newWidth  = Convert.ToInt32(image.Width * ratio);

                int posX = Convert.ToInt32((request.ThumbnailSize.Value.Width - (image.Width * ratio)) / 2);
                int posY = Convert.ToInt32((request.ThumbnailSize.Value.Height - (image.Height * ratio)) / 2);

                graphic.Clear(_options.ThumbnailBackground);
                graphic.DrawImage(image, posX, posY, newWidth, newHeight);


                System.Drawing.Imaging.ImageCodecInfo[] info =
                    ImageCodecInfo.GetImageEncoders();
                EncoderParameters encoderParameters;
                encoderParameters          = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
                                                                  _options.ImageQuality);


                thumbnail.Save(request.ThumbnailImagePath);
                image.Dispose();

                using (var fs = new FileStream(request.ThumbnailImagePath, FileMode.Open))
                {
                    await fs.CopyToAsync(stream);
                }
            }
        }