The National Gallery has succeeded in a £22m bid to buy Orazio Gentileschi’s The Finding of Moses after the public came to its help.

The masterpiece has been on loan to the gallery for nearly 20 years, so long that many assumed it was part of the permanent collection, said the director, Gabriele Finaldi.

In November, the gallery launched a public appeal to raise the last £2m for the purchase and on Wednesday it announced the target had been reached.

“It is great news as we come up to Christmas that this picture will be ours from early next year,” Finaldi said. “We are absolutely thrilled.”

The artwork is an enormous and glorious showstopper of a painting. It also has an important place in British history in that it was painted during Gentileschi’s 12-year residence in London at the court of Charles I and commissioned to celebrate the birth of the future Charles II.

The painting was for the Queen and hung in the Queen’s House in Greenwich. Many have speculated that the river scene on the right-hand side may represent Egypt and the Nile, but looks more like the Thames and the hill leading up to the Observatory. That would place the finding of Moses in his basket somewhere on the Isle of Dogs.

Finaldi called it “a very English picture” that was part of “that glittering constellation of artists and patrons at the court of Charles I in the 1630s. It was the most glorious moment of art patronage in this country when Rubens, Van Dyke and Orazio Gentileschi, and indeed Artemisia [his daughter] were rubbing shoulders in London at court.”

The owner and seller of the painting has been named by the Art Newspaper as Graham Kirkham, who made his fortune from his sofa chain DFS.

The gallery said the painting had been an acquisition priority since 1995 when it first attempted to purchase it.

Most of the funds came from the National Gallery’s own sources. The American Friends of the National Gallery gave £8.5m; £5m was from the National Gallery Trust; and £500,000from gifts it had been left in wills.

The National Heritage Memorial Fund gave £2.5m; the Art Fund gave one of its biggest single donations with £1m and the remaining £2m piece of the jigsaw came from the public, individuals and trusts.

Finaldi said: “From the small gifts of a few pounds to those of many thousands I am really thrilled that so many people have contributed in the last lap of the campaign.”

The gallery said the full cost of the painting was £22m, although the net cost was £19.5m because of the tax advantages of a private treaty sale.