[Tex/LaTex] Make background image in beamer fill full page while preserving aspect ratio

beamergraphics

I want to use a picture as a full-page background of a particular beamer slide.
I want the picture to fill the whole slide, but preserve its aspect ratio (which is different from that of the slide), in a manner that the picture gets centred (horizontally and vertically) on the slide and scaled up exactly to the point where the picture covers the whole slide (so the visible part gets cropped along the relatively longer side of the picture).

In other words, I want to replicate the "Fill Screen" behaviour commonly found for desktop backgrounds (such as in OS X).

What is a clean way to achieve the desired behaviour in beamer?


The following minimal working example
covers the background, but does not scale proportionally (first slide), or
scales the picture proportionally, but neither centres it, nor covers the whole background (second slide):

\documentclass{beamer}

\usepackage{graphicx}

\begin{document}

{\usebackgroundtemplate{%
\includegraphics[width=\paperwidth,height=\paperheight]{test.jpg}}
\begin{frame}
\end{frame}}

{\usebackgroundtemplate{%
\includegraphics[width=\paperwidth,height=\paperheight,keepaspectratio]{test.jpg}}
\begin{frame}
\end{frame}}

\end{document}

Slide 1:
Slide 1

Slide 2:
enter image description here

Link to test picture used

Best Answer

tigers

\documentclass{beamer}
\usepackage{tikz}
\usepackage{graphicx}

\begin{document}

{\usebackgroundtemplate{%
    \begin{tikzpicture}[remember picture, overlay]%
      \node at (current page.center) {\includegraphics[width=.1\paperwidth,height=.1\paperheight,keepaspectratio]{tiger}};%
    \end{tikzpicture}}
\begin{frame}
\end{frame}}
{\usebackgroundtemplate{%
    \begin{tikzpicture}[remember picture, overlay]%
      \node at (current page.center) {\includegraphics[width=\paperwidth,height=\paperheight,keepaspectratio]{tiger}};%
    \end{tikzpicture}}
\begin{frame}
\end{frame}}

\end{document}

cropped wildlife

\documentclass{beamer}
\usepackage{tikz}
\usepackage{graphicx}
\newlength{\picwidth}

\begin{document}

{\usebackgroundtemplate{%
    \settowidth{\picwidth}{\includegraphics[width=\paperwidth,height=\paperheight,keepaspectratio]{tiger}}%
    \ifdim\picwidth<\paperwidth
    \begin{tikzpicture}[remember picture, overlay]%
      \node at (current page.center) {\includegraphics[width=\paperwidth]{tiger}};%
    \end{tikzpicture}%
    \else
    \begin{tikzpicture}[remember picture, overlay]%
      \node at (current page.center) {\includegraphics[height=\paperheight]{tiger}};%
    \end{tikzpicture}%
    \fi
  }
\begin{frame}
\end{frame}}

\end{document}