In a city where kind words about anything American are hard to come by, one notable exception is a sand-colored, palm-fringed building formerly known as the American Center.

For two decades, Pakistanis still fondly reminisce, international newspapers were stacked high there, poetry and art were showcased and friendly American diplomats fostered a come-one-and-all vibe. But in 2005, amid rising anti-Americanism and Islamist militancy, the U.S. Embassy-run center in Islamabad became the last in a nationwide network to shut and the diplomats retreated to walled compounds.

The building, which now houses private media outfits, has since become a symbol of the gaping gulf between the United States and Pakistan — and a prime example, critics say, of how security measures at U.S. foreign missions since Sept. 11, 2001, have alienated American diplomats from the populations they most need to court.

Yet U.S. officials say the closures also ushered in a less tactile but more aggressive — and expensive — era in what they call “public diplomacy,” one facilitated by growth in electronic media and a surge in young Pakistanis using it. Largely from behind barbed wire, embassy staffers now wield a multimillion-dollar budget to stimulate debates on Facebook, fund English courses and provide research services to Pakistani students and officials. Most prominently, they are pumping money into programs that send Pakistanis to the United States, in hopes they will return as unofficial American ambassadors.

“This is what we are investing in,” said Mark Davidson, the U.S. Embassy’s public affairs counselor. He called the efforts “absolutely hard-edged and strategic.”

The mission is also daunting. Nuclear-armed Pakistan is a U.S. ally and major recipient of American aid. But the United States is seen here as a mercurial bully that, according to one recent poll, just 12 percent of Pakistanis view favorably.

Pakistan’s freewheeling media regularly spew what one U.S. diplomat called “terrible toxicity” about the United States. Though the U.S. Embassy staff has multiplied in recent years, diplomats rarely roam beyond major cities, or even much within them.

A 2010 State Department audit lamented that. It said that the Pakistani-staffed library “corners” that replaced the American centers received 28,000 visitors in 2009 but that “almost none of those visitors met an American and few participated in a program that would lead to a better understanding of the United States and U.S. policies.”

Some U.S. officials echo the concern. “A secure, open-access facility where people can meet and engage is still important,” one official said, adding that the closure of the American centers “hurt human interactions” between Americans and Pakistanis.

Last year, the embassy considered reopening the centers. But U.S. officials said they decided that the security situation in Pakistan, where a muscular Islamist insurgency has struck U.S. targets, meant that any potential structure would be more forbidding — and dangerous — than welcoming.

These days, the U.S. Consulate in Lahore features fuzzy photos of erstwhile American centers on its Facebook page. And U.S. diplomats argue that that page — along with other embassy Facebook pages, which together have 82,000 “fans” and play host to online debates — is one example of how technology fosters a broader American reach than a brick-and-mortar structure could. Through video conferencing, they say, visiting U.S. experts’ lectures have also been beamed to dozens of Pakistani universities.

U.S. officials say that in an era of CIA drone strikes and regular bilateral dust-ups, a rapid reversal of anti-Americanism is impossible. And so most of the jump in public-diplomacy spending in Pakistan — from $1.5 million in 2009 to $30 million in 2010 — funds educational and cultural exchanges, including the world’s largest Fulbright scholarship program. The program will take more than 2,000 Pakistanis to the United States this year, U.S. officials said.

Applications for those programs and for visas continue to rise despite the nations’ rocky relations, U.S. officials said. Eventually, Pakistanis who visit the United States might become “credible voices for a more reasoned and informed public opinion,” said the American diplomat, who was not authorized to speak for the record.

Madeeha Shams, who returned last month after completing a master’s in biochemistry as a Fulbright scholar in Upstate New York, agreed. She gushed that her American experience had taught her that “humans are humans everywhere,” and she said she strives to dispel the idea among some Pakistani friends that Americans have loose morals.

Still, “the best way to reduce antagonism would probably be for more people here to meet people from there,” said Shams, 25. Even that, she said, might do little to alter Pakistanis’ widespread disapproval of American policy.

On a recent morning, a handful of students checked Facebook accounts on computers at the U.S.-backed Lincoln Corner at Islamabad’s Islamic University, a suburban institution dotted with security checkpoints. The busiest of four such spaces in Pakistan, the library corner’s shelves hold Faulkner novels, a Disney encyclopedia and tomes on American football. Still, some students expressed surprise to learn that the site was U.S.-sponsored.

Sidra Shan, the library corner’s genial coordinator, said most students come to access the sophisticated research databases that the embassy has made available on the computers. The occasional presentations by visiting U.S. officials have helped clear up students’ “ambiguity” about the United States, she said.

Sania Zahra, an international relations student researching Saudi Arabia and Iran, said she admired Americans’ democratic values, as well as the organizational skills that, she said, make the Lincoln Corner a tidy refuge. But she said she felt part of a minority.

“The people of Pakistan say the Americans are fighting, they are doing drone attacks,” said Zahra, 25. “And where they are living, they are building high walls.”