Here's how Euler did this (from E675, translation available here). We start with
$$ \int_0^{\infty} x^{m-1} e^{-x} \, dx = \Gamma(m). $$
Changing variables gives
$$ \int_0^{\infty} x^{m-1} e^{-kx} \, dx = \frac{\Gamma(m)}{k^m}. $$
Euler now assumes that this still works if $k=p \pm iq$ is complex, provided $p>0$. (It does, but this needs an application of Cauchy's theorem.) We then have
$$ \int_0^{\infty} x^{m-1} e^{-(p \pm iq)x} \, dx = \frac{\Gamma(m)}{(p \pm iq)^m}, $$
and if we write $p=f\cos{\theta}$, $q=f\sin{\theta}$, we can apply Euler's formula to obtain
$$ \int_0^{\infty} x^{m-1} e^{-(p \pm iq)x} \, dx = \frac{\Gamma(m)}{f^m}(\cos{m\theta} \mp i\sin{m\theta}). $$
Taking the real part gives us
$$ \int_0^{\infty} x^{m-1} e^{-px} \cos{qx} \, dx = \frac{\Gamma(m)}{f^m}\cos{m\theta}, $$
and the result follows by inverting the expressions of $p$ and $q$ in terms of $f$ and $\theta$.