Ejemplo n.º 1
0
        public Bitmap Caption(Bitmap original, string caption, bool?appendDateTakenToCaption, string dateTaken, Rotations rotation, CaptionAlignments?captionAlignment, string fontName, float fontSize, string fontType, bool fontBold, Brush brush, Color backgroundColour, CancellationToken cancellationToken)
        {
            using (var logger = _logger.Block())
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                logger.Trace("Rotating image...");
                var rotated = _imageRotationService.Rotate(original, rotation);

                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                logger.Trace($@"Captioning image with ""{caption}""...");
                return(_imageCaptionService.Caption(rotated, caption, appendDateTakenToCaption, dateTaken, rotation, captionAlignment, fontName, fontSize, fontType, fontBold, brush, backgroundColour, cancellationToken));
            }
        }
Ejemplo n.º 2
0
        public Bitmap Caption(Bitmap image, string caption, bool?appendDateTakenToCaption, string dateTaken, Rotations?rotation, CaptionAlignments?captionAlignment, string fontName, float fontSize, string fontType, bool fontBold, Brush brush, Color backgroundColor, CancellationToken cancellationToken)
        {
            using (var logger = _logger.Block())
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                logger.Trace($"Creating a canvas {_canvasWidth}px x {_canvasHeight}px...");
                var canvas = new Bitmap(_canvasWidth, _canvasHeight);
                try
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }
                    logger.Trace($"Resizing image to fit canvas {_canvasWidth}px x {_canvasHeight}px...");
                    var aspectRatio = Math.Max((double)image.Width / _canvasWidth, (double)image.Height / _canvasHeight);
                    var imageHeight = (int)(image.Height / aspectRatio);
                    var imageWidth  = (int)(image.Width / aspectRatio);
                    var imageX      = (_canvasWidth - imageWidth) / 2;
                    var imageY      = (_canvasHeight - imageHeight) / 2;
                    using (var resizedImage = new Bitmap(imageWidth, imageHeight))
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return(null);
                        }
                        logger.Trace("Getting graphics manager for new image...");
                        using (var graphics = Graphics.FromImage(resizedImage))
                        {
                            logger.Trace("Setting up graphics manager...");
                            graphics.SmoothingMode      = SmoothingMode.HighQuality;
                            graphics.CompositingQuality = CompositingQuality.HighQuality;
                            graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;

                            logger.Trace("Drawing resized image...");
                            graphics.DrawImage(image, 0, 0, imageWidth, imageHeight);
                        }

                        logger.Trace($@"Adding caption ""{caption}"" to image...");
                        using (var resizedWithCaptionImage = _imageCaptionService.Caption(resizedImage, caption, appendDateTakenToCaption, dateTaken, rotation, captionAlignment, fontName, fontSize, fontType, fontBold, brush, backgroundColor, cancellationToken))
                        {
                            if (cancellationToken.IsCancellationRequested)
                            {
                                return(null);
                            }
                            logger.Trace("Getting graphics manager for canvas...");
                            using (var graphics = Graphics.FromImage(canvas))
                            {
                                logger.Trace("Setting up graphics manager...");
                                graphics.SmoothingMode      = SmoothingMode.HighQuality;
                                graphics.CompositingQuality = CompositingQuality.HighQuality;
                                graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;

                                logger.Trace("Drawing resized image...");
                                graphics.DrawImage(resizedWithCaptionImage, imageX, imageY, imageWidth, imageHeight);
                            }
                        }
                    }

                    return(canvas);
                }
                finally
                {
                    // release memory
                    if (cancellationToken.IsCancellationRequested)
                    {
                        canvas.Dispose();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public Bitmap Caption(Bitmap original, string caption, bool?appendDateTakenToCaption, string dateTaken, Rotations rotation, CaptionAlignments?captionAlignment, string fontName, float fontSize, string fontType, bool fontBold, Brush brush, Color backgroundColour, CancellationToken cancellationToken)
        {
            using (var logger = _logger.Block())
            {
                logger.Trace($"Creating a canvas {_canvasWidth}px x {_canvasHeight}px...");
                var canvas = new Bitmap(_canvasWidth, _canvasHeight);
                try
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }
                    logger.Trace("Rotating image...");
                    using (var rotated = _imageRotationService.Rotate(original, rotation))
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return(null);
                        }
                        logger.Trace($@"Resizing rotated image to fit a canvas {_canvasWidth}px x {_canvasHeight}px...");
                        var aspectRatio   = Math.Max(rotated.Height / (double)_canvasHeight, rotated.Width / (double)_canvasWidth);
                        var resizedHeight = Convert.ToInt32(original.Height / aspectRatio);
                        var resizedWidth  = Convert.ToInt32(original.Width / aspectRatio);
                        var resizedX      = (_canvasWidth - resizedWidth) / 2;
                        var resizedY      = (_canvasHeight - resizedHeight) / 2;
                        using (var resized = new Bitmap(resizedWidth, resizedHeight))
                        {
                            if (cancellationToken.IsCancellationRequested)
                            {
                                return(null);
                            }
                            using (var graphics = Graphics.FromImage(resized))
                            {
                                graphics.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                                graphics.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                                graphics.DrawImage(rotated, 0, 0, resizedWidth, resizedHeight);
                            }

                            using (var captioned = _imageCaptionService.Caption(resized, caption, appendDateTakenToCaption, dateTaken, rotation, captionAlignment, fontName, fontSize, fontType, fontBold, brush, backgroundColour, cancellationToken))
                            {
                                if (cancellationToken.IsCancellationRequested)
                                {
                                    return(null);
                                }
                                using (var graphics = Graphics.FromImage(canvas))
                                {
                                    graphics.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                                    graphics.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                                    graphics.DrawImage(captioned, resizedX, resizedY, resizedWidth, resizedHeight);
                                }
                            }
                        }
                    }

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }
                    return(canvas);
                }
                finally
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        canvas.Dispose();
                    }
                }
            }
        }