For more than 200 years it hung in genteel obscurity, a historic image of one of Scotland's most important historic characters.

Now an "historic" painting of Bonnie Prince Charlie by one the greatest Scottish portrait painters, worth more than £1m, has been acquired by the National Galleries of Scotland.

It has been hung for some time in, its former owner, the Earl of Wemyss and March says, in a "dark corridor, seen by few" in Gosford House, just outside Edinburgh.

Allan Ramsay's 18th century portrait of Prince Charles Edward Stuart is now part of the national collections due to the Acceptance in Lieu (AIL) of Tax scheme.

Once described as the "lost" Bonnie Prince Charlie portrait, it was brought to light by the art historian Dr Bendor Grosvenor.

The discovery was featured on a BBC 2 Culture Show special, The Lost Portrait of Bonnie Prince Charlie, two years ago.

It was painted a year before his defeat at the Battle of Culloden and is the only portrait of Bonnie Prince Charlie painted in Britain.

It has now formerly been transferred to ownership by the galleries from the Wemyss Heirlooms Trust - it was last exhibited in Edinburgh in 1946.

The amount of tax settled by the acceptance of the portrait, through the AIL system, is £1,122,838.33.

The painting will be displayed in Gallery 4 of the Scottish National Portrait Gallery in Edinburgh as a centrepiece to the gallery's collection of Jacobite art.

The Estate Duty for which the painting has been accepted in lieu of is that arising from the death of the 11th Earl on Wemyss, nearly 80 years ago, in 1937.

The present Earl, also known as Jamie Neidpath, or James Charteris, said: "The painting is a penetrating portrait of The Prince at the summit of his career, and captures brilliantly his unique character and situation.

"The Wemyss family have always appreciated its excellence, but it has usually hung in a dark corridor, seen by few.

"We therefore welcome the decision of HMRC and the Scottish government to acquire this painting for the Scottish National Portrait Gallery, as it will now be accessible to millions of viewers, who will thereby be enabled better to appreciate the nuances of personality of this iconic figure in Scottish and British history."

Prince Charles Edward Stuart (1720-1788) sought to re-capture the British throne for the House of Stuart during the ill-fated Rising of 1745.

He landed in Scotland on the 23rd of July, and marched to Edinburgh, defeating a government army at the Battle of Prestonpans.

Charles then travelled south as far as Derbyshire, before returning to Scotland; his army was eventually defeated at the Battle of Culloden on the 16th of April 1746.

The portrait is thought to have been created at Holyrood in Edinburgh during Bonnie Prince Charlie’s short time in the city at the height of the Rising, by Ramsay (1713-1784).

Ramsay was born in Edinburgh and studied in London, Rome and Naples, before returning to Scotland in 1738.

Bonnie Prince Charlie wears a powdered wig, has a velvet robe fringed with ermine, and the blue riband and star of the Order of the Garter.

The portrait was used as a prototype for painted and engraved versions, which were employed to promote the Jacobite cause.

Christopher Baker, director of the Scottish National Portrait Gallery, said: “This meticulous and dashing portrait is a work of great historical resonance, which in a real sense has now come home, as it will be celebrated as a key work in the nation’s Jacobite collection and as such become widely accessible.

"We are immensely grateful to everyone who has made its transference to public ownership, through the AIL scheme, possible.”

The Acceptance in Lieu (AIL) scheme allows those who have an inheritance tax bill to gift significant items to the nation and "satisfy" more tax than by selling items on the open market.

Edward Harley, the Acceptance in Lieu Panel Chairman, noted: "The Acceptance in Lieu Panel is pleased to have helped this iconic image of Bonnie Prince Charlie return to the city in which it was painted 270 years ago.

"It now takes its fitting place as one of the highlights of the great collection of the Scottish National Portrait Gallery where it can be enjoyed by all. This is indeed a unique moment in Scottish history."

The Acceptance in Lieu (AIL) scheme allows those who have an inheritance tax bill to gift significant items to the nation and "satisfy" more tax than by selling items on the open market.