Example #1
0
        protected override RequestedAction PreRenderImage(ImageState state)
        {
            if (state == null || !state.Data.ContainsKey(DataKey))
            {
                return(RequestedAction.None);
            }

            var data         = (AutoCropState)state.Data[DataKey];
            var settings     = (AutoCropSettings)state.Data[SettingsKey];
            var targetBox    = data.TargetDimensions;
            var instructions = data.Instructions;
            var size         = instructions.Size;

            if (data.ShouldPreRender)
            {
                // In this case another image should be rendered
                // This can be done by using the preRenderBitmap in the image state
                // If there isn't one already, or if it is of differing size, override it
                if (state.preRenderBitmap != null && state.preRenderBitmap.Size != size)
                {
                    state.preRenderBitmap.Dispose();
                    state.preRenderBitmap = null;
                }

                if (state.preRenderBitmap == null)
                {
                    state.preRenderBitmap = new Bitmap(size.Width, size.Height, state.sourceBitmap.PixelFormat);
                }

                // Since all bitmaps are created with 0 values after allocation
                // The proper background color needs to be pre-filled
                if (data.BytesPerPixel == 3)
                {
                    Raw.FillRgb(state.preRenderBitmap, data.BorderColor);
                }
                else if (data.BytesPerPixel == 4)
                {
                    Raw.FillRgba(state.preRenderBitmap, data.BorderColor);
                }

                // Copy pixels from original
                Raw.Copy(state.sourceBitmap, instructions.Target, state.preRenderBitmap, instructions.Translate);

                // Set the ImageResizer copyRect
                // This will crop the image in the render step
                state.copyRect = new RectangleF(0, 0, size.Width, size.Height);
            }
            else
            {
                state.copyRect = new RectangleF(state.copyRect.X + targetBox.X, state.copyRect.Y + targetBox.Y, state.copyRect.Width, state.copyRect.Height);
            }

            if (state == null || !settings.Debug)
            {
                return(RequestedAction.None);
            }

            try
            {
                var preRenderBitmap = state.preRenderBitmap ?? new Bitmap(state.sourceBitmap);

                if (settings.Method == AutoCropMethod.Edge)
                {
                    // Display graphics as analyzer sees it.
                    preRenderBitmap = Filter.Sobel(preRenderBitmap);
                }
                else
                {
                    preRenderBitmap = Filter.Buckets(preRenderBitmap);
                }

                // Establish a drawing canvas
                using (var graphics = Graphics.FromImage(preRenderBitmap))
                {
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;

                    // Render the detected bounding box in red
                    var brushSize = 2 * (1 / instructions.Scale);

                    using (var pen = new Pen(Color.Red, (int)brushSize))
                    {
                        var rectangle = instructions.Source;

                        if (data.ShouldPreRender)
                        {
                            var padding = data.Padding;
                            var width   = size.Width - padding.Width * 2;
                            var height  = size.Height - padding.Height * 2;
                            var offsetX = padding.Width;
                            var offsetY = padding.Height;

                            rectangle = new Rectangle(offsetX, offsetY, width, height);
                        }

                        graphics.DrawRectangle(pen, rectangle);
                    }
                }

                if (state.preRenderBitmap != null)
                {
                    state.preRenderBitmap.Dispose();
                }

                state.preRenderBitmap = preRenderBitmap;
            }
            catch (Exception)
            {
                // ignore
            }

            return(RequestedAction.None);
        }