Projected square magnified image using tikz

pgflayersspytikz-pgf

Is there a way to achieve magnification like the one shown below using Spy library in TikZ? If so, how to achieve it? If not so, what better alternatives are there?

The thick yellow outlines below are not necessary. But I do want to achieve solid boundaries and dashed projection lines.

magnification

My MWE:

\documentclass[border=3mm]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{spy}

\begin{document}
\begin{tikzpicture}

\begin{scope}[spy using outlines={rectangle,magnification=4, size=2.5cm,connect spies}]
    \node[rectangle,draw,inner sep=1pt] (image) at (0,0){\includegraphics[width=70px,height=70px]{image1.jpg}};
    \spy on (0,0) in node at (0,4);
\end{scope}
\end{tikzpicture}

\end{document}

produces

my-magnification

UPDATE 1

I managed to get the dashed square projection in the following way:

\documentclass[border=3mm]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{spy}

\begin{document}
\begin{tikzpicture}

\begin{scope}[spy using outlines={thick,red,rectangle,magnification=4, size=2.5cm,connect spies}]
    \node[rectangle,draw,inner sep=1pt] (image) at (0,0){\includegraphics[width=70px,height=70px]{image1.jpg}};
    \spy[spy connection path={
    \draw[densely dashed] (tikzspyinnode.north west) -- (tikzspyonnode.north west);
    \draw[densely dashed] (tikzspyinnode.south west) -- (tikzspyonnode.south west);
    \draw[densely dashed] (tikzspyinnode.north east) -- (tikzspyonnode.north east);
    \draw[densely dashed] (tikzspyinnode.south east) -- (tikzspyonnode.south east);
}] on (0,0) in node at (1.5,3.5);
\end{scope}
\end{tikzpicture}

\end{document}

which produces

my-magnification-2

The only problem is that the dashed lines cross over the magnified image as you can see. How to send them behind?

UPDATE 2

If I use the on background layer as scope option, it did solve for the magnifying part, but then again these lines go behind the actual image:

\documentclass[border=3mm]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{spy,backgrounds}

\begin{document}
\begin{tikzpicture}

\begin{scope}[spy using outlines={thick,red,rectangle,magnification=4, size=2.5cm,connect spies}]
    \node[rectangle,draw,inner sep=1pt] (image) at (0,0){\includegraphics[width=70px,height=70px]{image1.jpg}};
    \spy[fill=white,spy connection path={
    \begin{scope}[on background layer]
    \draw[densely dashed] (tikzspyinnode.north west) -- (tikzspyonnode.north west);
    \draw[densely dashed] (tikzspyinnode.south west) -- (tikzspyonnode.south west);
    \draw[densely dashed] (tikzspyinnode.north east) -- (tikzspyonnode.north east);
    \draw[densely dashed] (tikzspyinnode.south east) -- (tikzspyonnode.south east);
    \end{scope}
}] on (0,0) in node at (1.5,3.5);
\end{scope}
\end{tikzpicture}

\end{document}

my-magnification-3

Best Answer

An option using intersections:

RESULT:

enter image description here

MWE:

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{spy,intersections}

\begin{document}
    \begin{tikzpicture}
        
        \begin{scope}[spy using outlines={thick,red,rectangle,magnification=4, size=2.5cm,connect spies}]
            \node[rectangle,draw,inner sep=1pt] (image) at (0,0){\includegraphics[width=70px,height=70px]{example-grid-100x100pt.jpg}};
            \spy[spy connection path={
                \draw[densely dashed] (tikzspyinnode.north west) -- (tikzspyonnode.north west);
                \draw[densely dashed] (tikzspyinnode.south west) -- (tikzspyonnode.south west);
                \path[name path=Dline] (tikzspyinnode.north east) -- (tikzspyonnode.north east);
                \path[name path=SpyInBottomSide] (tikzspyinnode.south west) -- (tikzspyinnode.south east);
                \path [name intersections={of=Dline and SpyInBottomSide,by=I}];
                \draw[densely dashed,red](I) -- (tikzspyonnode.north east);
                \draw[densely dashed] (tikzspyinnode.south east) -- (tikzspyonnode.south east);
            }] on (0,0) in node at (1.5,3.5);
        \end{scope}
    \end{tikzpicture}
\end{document}