As written in the comment above, you simply could implement the image with captcha.png as source:
$("#captcha").html('<img src="captcha.png">');
no AJAX request would be required, but to fulfill your request:
Your PHP part could be something like:
// Enable output buffering
ob_start();
imagepng($png);
// Capture the output
$imagedata = ob_get_contents();
// Clear the output buffer
ob_end_clean();
echo base64_encode($imagedata);
And the AJAX part could look like
success : function(r){
$("#captcha").html('<img src="data:image/png;base64, '+ r +' ">');
}