I think I found the problem. It was saving the length of the document, not the number of pixels to read. I fixed that, now the number of pixels is saved. Secondly I think I found some problems in the actual saving of the length. I changed that as well