private void PositionWindow(Models.GridTargetLocation location)
        {
            const int BufferSize = 1;

            var diffX = 0.0;
            var diffY = HeaderHeight;

            this.WindowLeft   = location.ImageBounds.Left + location.Offset.X - diffX - BufferSize;
            this.WindowTop    = location.ImageBounds.Top + location.Offset.Y - diffY - BufferSize;
            this.WindowWidth  = (location.ImageBounds.Right - location.ImageBounds.Left) + diffX + 2 * BufferSize;
            this.WindowHeight = (location.ImageBounds.Bottom - location.ImageBounds.Top) + diffY + 2 * BufferSize;
        }
        public void SnapToImageBounds()
        {
            // select foreground window from several processes of supported applications
            var nativeWindowsList = Models.AppsInterop.NativeWindow.GetWindowsInTopMostOrder();

            var nativeWindows = nativeWindowsList
                                .Select(w => new Models.NativeWindowState
            {
                Handle    = w.Handle,
                ClassName = w.ClassName,
                Width     = w.Rect.Width,
                Height    = w.Rect.Height,
                Caption   = w.Title,
            })
                                .ToList();

            if (nativeWindows.Count > 0)
            {
                // TODO: refactor - move priority logic to model
                if (String.Compare(nativeWindows[0].ClassName, Models.AppsInterop.PhotoViewerWindow.MainWindowClassName, StringComparison.Ordinal) == 0)
                {
                    var photoViewerWindow = new Models.AppsInterop.PhotoViewerWindow(nativeWindows[0].Handle);

                    var rectViewedImage = photoViewerWindow.PhotoCanvasRect();
                    if (!rectViewedImage.IsEmpty)
                    {
                        var location = new Models.GridTargetLocation {
                            ImageBounds = rectViewedImage, Offset = Models.Geometry.Point.Zero,
                        };
                        this.PositionWindow(location);
                    }
                }
                else
                {
                    var nativeWindow = nativeWindows[0];

                    var isOctaneRender = false;
                    if (Models.AppsInterop.OctaneRenderWindow.GetFromAllProcesses().Count > 0)
                    {
                        var w = SelectOctaneRenderStandaloneMainWindow(nativeWindows, Models.AppsInterop.OctaneRenderWindow.GetFromAllProcesses()[0].ClassName);
                        if (w != null)
                        {
                            nativeWindow   = w;
                            isOctaneRender = true;
                        }
                    }

                    var selectedNativeWindow = new Models.AppsInterop.NativeWindow(nativeWindow.Handle);
                    var bitmap = selectedNativeWindow.GetShot();

                    Task.Factory.StartNew(() =>
                    {
                        var flatImage = new Models.FlatImage(bitmap);

                        Models.Geometry.Rectangle imageBounds;
                        if (isOctaneRender)
                        {
                            imageBounds = Models.AppsInterop.OctaneRenderWindow.FindRenderedImageBorders(flatImage);
                        }
                        else
                        {
                            imageBounds = flatImage.FindBoundsOfInnerImage();
                        }

                        var nativeWindowLocation = new Models.Geometry.Point(selectedNativeWindow.Location.X, selectedNativeWindow.Location.Y);
                        return(new Models.GridTargetLocation {
                            ImageBounds = imageBounds, Offset = nativeWindowLocation,
                        });
                    }).ContinueWith((t) =>
                    {
                        if (!t.Result.ImageBounds.IsEmpty)
                        {
                            if ((t.Result.ImageBounds.Width > 150) && (t.Result.ImageBounds.Height > 50))
                            {
                                this.PositionWindow(t.Result);
                            }
                        }
                    },
                                    TaskScheduler.FromCurrentSynchronizationContext());
                }
            }
        }