Use PwriteFully and write image header last
am: dba5a70977

* commit 'dba5a70977da0a28cec2bfc8261d52a177738477':
  Use PwriteFully and write image header last