Were Batman Begins or The Dark Knight based on any particular comic(s) or was were they completely created from scratch?
Batman Begins is a combination of stories from the Batman mythos, as well as an original story. According to Wikipedia, the starting point for Batman Begins was a story called "The Man Who Falls"; Jim Gordon was based on the character from the story "Batman: Year One". Neither of these stories featured Ra's Al Ghul, who is a significantly different character than the one featured in the comic (both Ra's are leaders of a large group of assassins, and both have a large respect for Batman, but the Ra's in the comics is depicted as being hundreds of years old, regenerating himself in a pool of liquid called "The Lazarus Pit". It remains to be seen if the Ra's in the movie makes a similar comeback or not).
The film The Dark Knight uses elements from the story The Long Halloween, though again the story is mainly an original story. It was also related that Heath Ledger, who played The Joker, locked himself in his hotel room for weeks to try and get to the core of the character of The Joker, even writing a journal of The Joker's 'thoughts'.
The stories of all the three movies incorporate many influences from famous comic storylines, while none of them has been directly taken from a particular comic in its entirety. So they were more or less written from scratch a bit but also not completey without base in some particular comics.
The primary influence, maybe less storywise than more atmospherically and setting-wise, that screenwriters Christopher Nolan and David S. Goyer identify for the first two movies (if not even the whole trilogy), however, seems to be Jeph Loeb's The Long Halloween. While it has found its way most directly into The Dark Knight, its themes waft over the entire trilogy. While featuring many of Batman's classic villains in side-plots, at its very core The Long Halloween tells a noire story of a Gotham that is controlled by organized crime and a feud between the Falcones and Maronis and three righteous men (Dent, Gordon and the Batman) working together as a team in trying to put an end to their realm. And also its art that uses clear and monotonous colors and strong light-shadow contrasts plays into this noire feel to a large degree and might have been especially appealing to noire-enthusiast Christopher Nolan in this regard. It is thus a great starting point for their effort in grounding Batman into a serious crime drama. A recent edition of The Long Halloween features a foreword by Nolan and Goyer where they discuss its influence on the movies, reinforcing the above points:
Now let's look into the individual movies and the particular story elements and their sources a bit more.
A primary milestone you won't get around when talking about how Batman Begins is of course Frank Miller's Year One, which was part of the modernization at the end of the 80s and depicted Batman's first steps in Gotham. But not only that, it also laid much emphasis on James Gordon's first steps, too, and the corrupt city and police he has to face as one of the few honest cops, a motif Batman Begins also played with quite a bit. It thus introduced themes and characters that The Long Halloween directly built upon. While Year One doesn't see Gordon and Batman work directly together, it does put them as two equal men trying to fight their way through the dump that this city is and The Long Halloween then introduced the D.A. as third part in the team, a notion very eminent in Batman Begins:
And the image of James Gordon that Year One introduced and The Long Halloween built upon has also directly influenced his depiction in the movie:
As to particular plot points, there are some scenes from Year One that found their way into Batman Begins, like Batman raiding a deal that Flass is supervising, his usage of a swarm of ultra-sonically attracted bats as cover to escape a SWAT team, or the last scene where Gordon has his first "official" meeting with the Batman, examining a Joker card.
While Year One starts with Batman arriving in Gotham, it leaves out much of his previous past. This is the point where Dennis O'Neil's The Man Who Falls steps in and from which many parts are used for Bruce's story before his return to Gotham. We have Bruce falling into the well on Wayne Manor and his first encounter with the bats. We see him traveling the world and learning martial arts in a desolate Asian monastery. And later on Bruce is also trained in the powerful weapons of "theatricality and deception" by the mercenary Henri Ducard (though, fused in the movie with Ra's al Ghul and his training in the monastery), whom he abandons after learning how brutal his methods really are.
Or from the mouth of the screenwriters directly (as captured on the BluRay of Batman Begins):
(I'm also tempted to say that the image of Scarecrow riding on a horse was taken from The Long Halloween, too.)
In The Dark Knight we again have a large story- or character-wise influence from The Long Halloween in the way it concentrates on Harvey Dent, his work with Gordon and the Batman, his demise, and his actions and motivations after his "transformation", putting him on a revenge quest after the crime bosses and people who betrayed him, who lays justice rather into the hands of a coin flip instead of the supposedly flawed system. Another smaller story-similarity is when Batman and Dent are trying to grip the mob where it hurts, its money (though in the comic they rather chose the approach the Joker does in the movie, burning it all, instead of confiscating).
Now when talking about the Joker there's no way around Alan Moore's The Killing Joke and indeed the movie borrows many major elements from it. In that story the Joker tries to prove that "all it takes is one bad day to reduce the sanest man alive to lunacy". In the story he takes Gordon as a target and the shooting of his daughter as a means, while the movie primarily uses Dent instead, but the idea is exactly the same, the Joker wants to prove that the world is full of insanity and random injustice and has to drive anyone mad sooner or later. And while in the comic he doesn't succeed in his efforts with Gordon, who still wants to bring him in "by the book" after all this, he succeed to some degree with the Batman, if we accept the common interpretation of the comic's ending that
There are also other aspects taken from this comic, like the Joker's assertion of Batman being as crazy as he is and the whole idea of them fighting their fight forever and ever. And while the comic does tell some backstory of the Joker, the final character in his randomness does not actually adhere to any particular backstory and "prefers it to be multiple choice". This is reflected in the movie Joker's retelling of the origin of his scars in multiple different versions.
(The last scene of the movie, however, is yet again a little reminiscent of the finale of Year One (albeit varying in many details), where Bruce (not Batman, though) rescues Gordon's newborn son from falling down a bridge while Gordon fights with the kidnapper.)
While you didn't really ask for The Dark Knight Rises, it would be a shame to omit it here for a complete answer. Now when talking about Batman coming back from a near decade of retirement, there's no way around Frank Miller's The Dark Knight Returns. But there's more to it than just this premise. We also have the emphasis on the public controversy around Batman (even though to a lesser degree in the movie, but still most prominent in the 3rd movie). Then there are smaller plot elements like Batman's problems due to his age, especially when he tries to take on a much younger and stronger opponent against Alfred's warnings, or his employment of a mechanical splint to make up for the deficiencies of his aging body. And of course there's also the up-beat ending of faking his own death but leaving his legacy to a successor.
Then when looking at the character of Bane and his methods and actions, we have the Knightfall storyline. While the details are quite a bit different, it shows Bane wanting to rule the city and taking the fight to a weakened Batman, whom he deduced to be Bruce Wayne, and ultimately breaking his back, forcing him into exile, where he then heals from his injury.
In addition to that the movie's story, especially the whole scenario in Gotham after Bane's attack, is reminiscent of No Man's Land, which sees Gotham cut from the outside world (though, by an earthquake) and devolving into a primitive and chaotic state, especially after Batman leaves for months. It also has Lex Luthor trying to cover up a financial scheme by employing Bane as a mercenary (a role filled by John Daggett in the movie).