Fixing image writing to in memory output stream