Rony Pierre-Louis, 51, was charged with public indecency and held on $25,000 bond.
The man allegedly did his deed at the public beach in full view of a woman, who quickly took a photograph of him as he was doing so and then took a photo of his vehicle's license plate, police said.
Police commended her response.
"Because of her quick thinking, the victim was able to get this unimpressive Kodak moment and his license plate photographed, which was huge in helping us identify him," Sgt. Kelly Connelly said.
Officers put together a photo lineup and the witness was able to identify Pierre-Louis, police said.
Police applied for a warrant for his arrest, and he was arrested at the Stamford train station, where he is employed as a security guard, police said.
Click here to follow Daily Voice Stamford and receive free news updates.