Were Batman Begins or The Dark Knight based on any particular comic(s) or 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 reign. And its art that uses clear and monotonous colors and strong light-shadow contrasts also 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:
CN: When you're putting together a Batman film, people always ask, "Are you looking at this comic book or that comic book?" And the truth is you look at all of them. As a filmmaker, though, THE LONG HALLOWEEN was one Batman story that really drew me in in terms of cinematic potential...It's a crime epic. Jeph Loeb did this incredible job of taking the more exotic elements of the Batman universe and grounding them in a believable world. He took supporting characters and gave them real lives and real emotions. And real consequences to their actions. This has tremendous impact on the reader. THE LONG HALLOWEEN in more than a comic book. It's an epic tragedy...Along those lines I was impressed with how seamlessly Loeb and Sale were able to integrate the more fantastical elements of Batman, most notably the villains, within the context of the real world, striking a balance that felt credible. It was a great inspiration to us in terms of tonality.
DG: For me there are three major comic book influences in Batman lore. There's YEAR ONE, the Neal Adams stuff, and there is THE LONG HALLOWEEN. But by the time The Dark Knight comes out, it will become apparent that LONG HALLOWEEN is the preeminent influence on both movies.
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:
CN: Well, we co-opted THE LONG HALLOWEEN's idea of the triumvirate for Batman Begins to some extent, showing Batman function as one point of a strong triangle with the police and the D.A. by acting as a force that can crack things open and provide a wedge against all the corruption.
DG: That scene on the rooftop between Gordon, Dent, and Batman in THE LONG HALLOWEEN -- in which you realize Batman can obviously bring criminals to justice, but he needs the police to arrest them and the D.A.'s office to prosecute them -- that was something new that Jeph Loeb introduced into the lore. For Batman Begins we used Rachel instead of Dent as the pinnacle of the triangle, but she still served the same function.
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:
DG: THE LONG HALLOWEEN really ran with what YEAR ONE started, giving us an entirely different depiction of Gordon. Previously in the comic books, in the movies, and in the TV show, Gordon was this kind of bumbling, avuncular character, whereas in these stories, he is depicted as this beleaguered, mid-level sergeant in this rampantly corrupt police force, which is the Gordon we meet in Batman Begins.
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):
Chistopher Nolan: The jumping-off point really for me had been a story called "The Man Who Falls". It suggests various points on the development of Bruce Wayne into Batman. This idea that he travels the world is mentored and teached in different disciplines by various individuals and then returns to Gotham for me was a fascinating story, was a fascinating connection between Bruce's early childhood trauma, falling down this well and being attacked by these bats, and the persona that he then develops in order to use people's fear against them.
David S. Goyer: Another piece of work that influenced us was Frank Miller's Batman: Year One because that did deal with the first year of Batman being Batman. And the approach was very no-nonsense and very tough. And it was a great relationship that Frank Miller developed between Bruce Wayne, the beginnings of Batman, and Gordon, who is not yet Commissioner Gordon.
(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 succeeds to some degree with the Batman, if we accept the common interpretation of the comic's ending that
Batman kills the Joker. (And at the end of the movie he also has to kill.)
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).