If those two snippets in the OP are in the same script, you can add a few more lines to use MD5 checksums on $raw_string and $decoded, to confirm whether the encode/decode is behaving as it should:
use strict; use warnings; use MIME::Base64; use Digest::MD5 'md5_hex'; open (my $image, 'Z:\capture.png') or die "$!"; binmode $image; my $raw_string = do{ local $/ = undef; <$image>; }; my $encoded = encode_base64( $raw_string ); my $decoded = MIME::Base64::decode_base64($encoded); my $rawmd5 = md5_hex( $raw_string ); my $decmd5 = md5_hex( $decoded ); warn sprintf( "raw != decoded (%s vs %s)\n", $rawmd5, $decmd5 ) if ( $rawmd5 ne $decmd5 ); open my $fho, '>', 'Z:\photo.png' or die $!; binmode $fho; print $fho $decoded;
(I added binmode on the input file handle, just in case; but if that's the problem with the OP code, then I wouldn't expect your input and output files to be exactly the same size.)

In reply to Re: image encode base64 - help needed by graff
in thread image encode base64 - help needed by zurich

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.