Example #1
0
        /// <summary>
        /// Handles the request for an image.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private bool ServeImage(OwinContext context)
        {
            var handled      = false;
            var imageRequest = ExtractImageRequest(context);
            var result       = imageRequest != null;

            if (result)
            {
                Image stockImage = null;
                Image tempImage  = null;

                try {
                    result = BuildInitialImage(context, imageRequest, ref stockImage, ref tempImage);

                    if (result)
                    {
                        var configuration = _SharedConfiguration.Get();

                        if (imageRequest.IsHighDpi)
                        {
                            tempImage = _Graphics.UseImage(tempImage, _Graphics.ResizeForHiDpi(tempImage ?? stockImage));
                        }
                        if (imageRequest.RotateDegrees > 0.0)
                        {
                            tempImage = _Graphics.UseImage(tempImage, _Graphics.RotateImage(tempImage ?? stockImage, imageRequest.RotateDegrees.Value));
                        }
                        if (imageRequest.Width != null)
                        {
                            tempImage = _Graphics.UseImage(tempImage, _Graphics.WidenImage(tempImage ?? stockImage, imageRequest.Width.Value, imageRequest.CentreImageHorizontally));
                        }
                        if (imageRequest.ShowAltitudeStalk)
                        {
                            tempImage = _Graphics.UseImage(tempImage, _Graphics.AddAltitudeStalk(tempImage ?? stockImage, imageRequest.Height.GetValueOrDefault(), imageRequest.CentreX.GetValueOrDefault()));
                        }
                        else if (imageRequest.Height != null)
                        {
                            tempImage = _Graphics.UseImage(tempImage, _Graphics.HeightenImage(tempImage ?? stockImage, imageRequest.Height.Value, imageRequest.CentreImageVertically));
                        }

                        var addTextLines = imageRequest.HasTextLines && (!context.IsInternet || configuration.InternetClientSettings.CanShowPinText);
                        if (addTextLines)
                        {
                            tempImage = _Graphics.UseImage(tempImage, _Graphics.AddTextLines(tempImage ?? stockImage, imageRequest.TextLines, centreText: true, isHighDpi: imageRequest.IsHighDpi));
                        }
                    }

                    if (result)
                    {
                        var image = tempImage ?? stockImage;

                        if (image != null)
                        {
                            using (var stream = new MemoryStream()) {
                                using (var copy = (Image)image.Clone()) {
                                    copy.Save(stream, imageRequest.ImageFormat);
                                    var bytes = stream.ToArray();

                                    context.ResponseHttpStatusCode = HttpStatusCode.OK;
                                    context.ReturnBytes(
                                        ImageMimeType(imageRequest.ImageFormat),
                                        bytes
                                        );

                                    handled = true;
                                }
                            }
                        }
                    }
                } finally {
                    if (stockImage != null)
                    {
                        stockImage.Dispose();                            // clones are now made of all stock images
                    }
                    if (tempImage != null)
                    {
                        tempImage.Dispose();
                    }
                }
            }

            return(handled);
        }
Example #2
0
        /// <summary>
        /// See base class.
        /// </summary>
        /// <param name="server"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected override bool DoHandleRequest(IWebServer server, RequestReceivedEventArgs args)
        {
            bool result = false;

            if (args.PathAndFile.StartsWith("/Images/", StringComparison.OrdinalIgnoreCase))
            {
                var imageRequest = ExtractImageRequest(args);
                result = imageRequest != null;

                // Stock image is an image from the resource file. We don't need to dispose of it, it can be reused
                // Temp image is an image we have either built from scratch or built by modifying a stock image. It is only
                // good for this request and needs to be disposed of when we're done with it.
                Image stockImage = null;
                Image tempImage  = null;

                if (result)
                {
                    try {
                        result = BuildInitialImage(imageRequest, args, ref stockImage, ref tempImage);
                        if (result)
                        {
                            if (imageRequest.IsHighDpi)
                            {
                                tempImage = _Graphics.UseImage(tempImage, _Graphics.ResizeForHiDpi(tempImage ?? stockImage));
                            }
                            var addTextLines = imageRequest.HasTextLines && (!args.IsInternetRequest || _InternetClientCanShowText);
                            if (imageRequest.RotateDegrees > 0.0)
                            {
                                tempImage = _Graphics.UseImage(tempImage, _Graphics.RotateImage(tempImage ?? stockImage, imageRequest.RotateDegrees.Value));
                            }
                            if (imageRequest.Width != null)
                            {
                                tempImage = _Graphics.UseImage(tempImage, _Graphics.WidenImage(tempImage ?? stockImage, imageRequest.Width.Value, imageRequest.CentreImageHorizontally));
                            }
                            if (imageRequest.ShowAltitudeStalk)
                            {
                                tempImage = _Graphics.UseImage(tempImage, _Graphics.AddAltitudeStalk(tempImage ?? stockImage, imageRequest.Height.GetValueOrDefault(), imageRequest.CentreX.GetValueOrDefault()));
                            }
                            else if (imageRequest.Height != null)
                            {
                                tempImage = _Graphics.UseImage(tempImage, _Graphics.HeightenImage(tempImage ?? stockImage, imageRequest.Height.Value, imageRequest.CentreImageVertically));
                            }
                            if (addTextLines)
                            {
                                tempImage = _Graphics.UseImage(tempImage, _Graphics.AddTextLines(tempImage ?? stockImage, imageRequest.TextLines, centreText: true, isHighDpi: imageRequest.IsHighDpi));
                            }
                        }

                        if (result)
                        {
                            Responder.SendImage(args.Request, args.Response, tempImage ?? stockImage, imageRequest.ImageFormat);
                            args.Classification = ContentClassification.Image;
                        }
                    } finally {
                        if (stockImage != null)
                        {
                            stockImage.Dispose();                           // clones are now made of all stock images
                        }
                        if (tempImage != null)
                        {
                            tempImage.Dispose();
                        }
                    }
                }
            }

            return(result);
        }