Recent

Author Topic: [Solved] PNGImageException 'Chunk length exceeds stream length'  (Read 1168 times)

lainz

  • Hero Member
  • *****
  • Posts: 3957
  • Leandro Diaz
[Solved] PNGImageException 'Chunk length exceeds stream length'
« on: December 30, 2019, 09:30:17 pm »
I'm getting the error PNGImageException 'Chunk length exceeds stream length'


I'm using the unit posted here:
https://forum.lazarus.freepascal.org/index.php/topic,47901.msg344243.html#msg344243

With this code I can not replicate the error, but is the same I'm doing in my application that's really big and propietary so I can not post it, but this is the intended use:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   LBuffer : rawbytestring;
  4.   stream: TFileStream;
  5.   s: TStringList;
  6. begin
  7.   LBuffer := #27'@'; // Initialise the printer
  8.   s := TStringList.Create;
  9.   s.LoadFromFile(Application.Location + 'logo.txt');
  10.   LBuffer := LBuffer + _ESCPosPrintBitmap().RenderBitmapBase64(s.Text);
  11.   s.Free;
  12.  
  13.   stream := TFileStream.Create('logo.bin', fmCreate);
  14.   try
  15.     stream.Write(LBuffer[1], Length(LBuffer));
  16.   finally
  17.     stream.Free;
  18.   end;
  19. end;

Whit this base64 image:
Code: Pascal  [Select][+][-]
  1. data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQQAAAEECAYAAADOCEoKAAAR/klEQVR4Xu2d0XYjOQhEk///6OxxZu04ju1GCEShvvO2J4KGoriWO7O7n19fX18f/EEBFECBj4+PT4CAD1AABa4KAAS8gAIocFMAIGAGFEABgIAHUAAF/irADeHErvj8/Py4f6d8+efLH94zn9cUAOGks3+Ewb0M7352UrlO0zZAOM2ofxq1LLzlzAml275lgLD9iH83OLLoI2dPJuO27QKEbUf75IXRwzsDS+tAwaLSPmcAwj6zfNvJzGLPxJ5E3m3aBAjbjPJ1IxELHZHjBFK3bxEgtB/h+wYiFzky1+ayt20PILQd3XHhGQuckfO4E06sUgAgrFJ68XMyFzcz92KZeNyDAgBhQ0usWNgVz9hwNPItAQT5EY0VuHJRVz5rTA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v0MQipQgAIwCBllaRKAUCp/3MOrgQAM4mZZmQkgVKof/OwqKACD4EEWpgMIheJHP7oCCMAgeoq1+QBCrf7hT18JBWAQPr7yhAChfATxBayAAjCIn5tCRoCgMIWEGjKhAAwSBiaSEiCIDCKjjAwoAIOMSenkBAg6s0ipJBIKwCBlRFJJAYLUOHKKiYACMMiZjVpWgKA2kaR6ZqAADJKGIpgWIAgOJaskDxSAQdY0NPMCBM25pFU1AgVgkDYG2cQAQXY0eYVZoAAM8vRXzgwQlKeTXNszMACCZNHF0wME8QFRHgqsVAAgrFSbZ6GAuAIAQXxAlIcCKxUACCvV5lkoIK4AQBAfEOWhwEoF/gPcEwq4O5VcVQAAAABJRU5ErkJggg==

The application is in mode objfpc, the ssESCPosPrintBitmap.pas from the other post is in mode delphi.

What can be causing the issue, I tested the image and it's a valid PNG image... any ideas?
« Last Edit: December 30, 2019, 10:55:48 pm by lainz »
https://lainz.github.io/ - My Website :)
https://lazpaint.github.io/ -  Download LazPaint

circular

  • Hero Member
  • *****
  • Posts: 3666
    • Personal webpage
Re: PNGImageException 'Chunk length exceeds stream length'
« Reply #1 on: December 30, 2019, 09:45:19 pm »
I suspect a bit of the stream is not saved for some reason.
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 3957
  • Leandro Diaz
Re: PNGImageException 'Chunk length exceeds stream length'
« Reply #2 on: December 30, 2019, 09:54:43 pm »
What puzzles me is that in the demo project everything works fine, but when I want to incorporate to the application the error occurs.
https://lainz.github.io/ - My Website :)
https://lazpaint.github.io/ -  Download LazPaint

Cyrax

  • Hero Member
  • *****
  • Posts: 839
Re: PNGImageException 'Chunk length exceeds stream length'
« Reply #3 on: December 30, 2019, 10:26:05 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   IssESCPosPrintBitmap = interface( IInterface )
  3.       ['{3F279585-6D2E-451F-AF97-76F0E07A70DF}']
  4.       function RenderBitmap( const ABitmapFilename: string ): rawbytestring;
  5.       function RenderBitmapBase64(const base64data: string): rawbytestring;
  6.   end;
  7.  

What about changing all occurrences of string type to rawbytestring one? Does that help?

lainz

  • Hero Member
  • *****
  • Posts: 3957
  • Leandro Diaz
Re: PNGImageException 'Chunk length exceeds stream length'
« Reply #4 on: December 30, 2019, 10:37:54 pm »
Hi, it does nothing. :S

Also the function DecodeStringBase64(const s:string;strict:boolean=false):String; expects and returns strings.
https://lainz.github.io/ - My Website :)
https://lazpaint.github.io/ -  Download LazPaint

winni

  • Hero Member
  • *****
  • Posts: 2196
Re: PNGImageException 'Chunk length exceeds stream length'
« Reply #5 on: December 30, 2019, 10:51:38 pm »
Hi!

Try to save the file without a stream but the old fashioned way:

Code: Pascal  [Select][+][-]
  1. var
  2. f : file of byte;  
  3.  
  4. .....
  5. assignFile(F,application.location+'logo.bin');
  6. rewrite(f);
  7. for i := 1 to length(LBuffer) do write(f,ord(LBuffer[i]));
  8. closeFile(f);                                              


Look if now still an error occurs.

Winni

lainz

  • Hero Member
  • *****
  • Posts: 3957
  • Leandro Diaz
Re: PNGImageException 'Chunk length exceeds stream length'
« Reply #6 on: December 30, 2019, 10:55:38 pm »
Thanks. I found the problem and it was outside the scope of the demo project, sorry  :o

The problem was that the TBufDataset for some reason loaded only a part of the base64 string.

Changed that line and it works.
https://lainz.github.io/ - My Website :)
https://lazpaint.github.io/ -  Download LazPaint

 

TinyPortal © 2005-2018