Recent

Author Topic: error in debug when using dbimage  (Read 4801 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
error in debug when using dbimage
« on: October 30, 2015, 09:53:32 am »
I'm converting my delphi demo to lazarus 1.44 linux (mint). In a form I load an image from a database. In debug mode this will occur an error. But the application goes on and there's is no AV. So everything works fine (at the moment).

Did I notised a bug?

My resources are:
- linux
  RELEASE=17.1
  CODENAME=rebecca
  EDITION="MATE 64-bit"
  DESCRIPTION="Linux Mint 17.1 Rebecca"
  DESKTOP=MATE
  TOOLKIT=GTK
- Lazarus 1.4.4 / FPC 2.6.4
- embedded firebird 2.5
- ZEOS 7.1X
« Last Edit: October 30, 2015, 10:23:10 am by mangakissa »
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: error in debug when using dbimage
« Reply #1 on: October 31, 2015, 10:11:28 pm »
How was the image saved to the database?

If it was saved as raw image and not using TDBImage but using BlobStream for example you can't just read the image via TDBImage. Default the TDBImage writes the extension before the image (if the WriteHeader is set to true).

If the image is saved without the extension in front of it you need to implement the OnDBImageRead to return the correct extension of the image (.jpg/.bmp etc).

If the image was written with TDBImage are you sure the WriteHeader was set to true at the time?

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: error in debug when using dbimage
« Reply #2 on: November 02, 2015, 12:31:45 pm »
I don't know. It's the same database I'm using the same database with Delphi XE7. As far as I know the image is written with DBImage with ZEOS. What do you mean with WriteHeader?
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: error in debug when using dbimage
« Reply #3 on: November 02, 2015, 12:41:27 pm »
What do you mean with WriteHeader?
In Lazarus there is a header written before the actual image. The header contains the extension of the image (jpg/bmp/png etc) so the component knows what kind of image it is. The WriteHeader is true by default so in Lazarus the TDBImage writes this string before the actual data by default. For reading the WriteHeader is not used.

When reading data from the database the TDBImage looks if you implemented TDBImage.OnDBImageRead. If you did you need to provide the GraphExt-parameter with the correct image-type. After that TDBImage reads the image raw from the database. If you didn't implement TDBImage.OnDBImageRead then TDBImage expects the GraphExt-string to be part of the actual data in the database.

Related code:
Code: Pascal  [Select][+][-]
  1. procedure TDBImage.LoadPicture;
  2. ...
  3.         try
  4.           AGraphic := nil;
  5.           GraphExt := '';
  6.           if assigned(FOnDBImageRead) then
  7.             begin
  8.             // External method to identify graphic type
  9.             // returns file extension for graphic type (e.g. jpg)
  10.             // If user implements OnDBImageRead, the control assumes that
  11.             // the programmer either:
  12.             //
  13.             // -- Returns a valid identifier that matches a graphic class and
  14.             //    the remainder of stream contains the image data. An instance of
  15.             //    of graphic class will be used to load the image data.
  16.             // or
  17.             // -- Returns an invalid identifier that doesn't match a graphic class
  18.             //    and the remainder of stream contains the image data. The control
  19.             //    will try to load the image trying to identify the format
  20.             //    by it's content
  21.             //
  22.             // In particular, returning an invalid identifier while the stream has
  23.             // a image header will not work.
  24.             OnDBImageRead(self,s,GraphExt);
  25.             GraphExtToClass;
  26.             end
  27.           else
  28.             ReadImageHeader;

I don't think you wrote the extension in Delphi before the image. I don't think the TDBImage in Delphi has this extension capability. So, to keep it compatible, you need to implement that OnDBImageRead and provide the GraphExt. And when writing the image back to the database you need to set WriteHeader to false.

Only thing is that you need to provide the GraphExt and if you don't know which type is saved then you don't know what to provide. But you had the same trouble in Delphi :)

If the image is always jpg that isn't really a problem.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: error in debug when using dbimage
« Reply #4 on: November 02, 2015, 12:50:10 pm »
What do you mean with WriteHeader?
In Lazarus there is a header written before the actual image. The header contains the extension of the image (jpg/bmp/png etc) so the component knows what kind of image it is.
which makes it delphi compatible.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: error in debug when using dbimage
« Reply #5 on: November 02, 2015, 01:19:08 pm »
What do you mean with WriteHeader?
In Lazarus there is a header written before the actual image. The header contains the extension of the image (jpg/bmp/png etc) so the component knows what kind of image it is.
which makes it delphi compatible.
Are you saying TDBImage in Delphi also writes the extension before the image to the database?

I'm on Delphi XE and didn't think that happened but am not sure.

I think setting WriteHeader to false makes it Delphi compatible but when loading you need to implement the OnDBImageRead to set the GraphExt. Or are you saying it is the other way around?

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: error in debug when using dbimage
« Reply #6 on: November 02, 2015, 02:51:05 pm »
Just tested TDBImage in Delphi XE and unless they broke compatibility in later Delphi versions this still holds.

TDBImage (in Delphi XE) does NOT save the extension before the image-data. I could load a jpg in a TDBImage but it didn't save to the DB. Only bmp was saved automatically. In the raw data of the field there was no extension indication.

So for compatibility with Delphi you need to set WriteHeader to false and implement OnDBImageRead for reading to provide the correct GraphExt. If you don't provide a OnDBImageRead, Lazarus tries to read the extension and when it's not there it will fail with an error reading from stream.

If you use WriteHeader=true and don't implement OnDBImageRead, Lazarus will take care of the image-detection (via the Ext-string which is written before the image-date). But obviously this is not compatible with TDBImage in Delphi.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: error in debug when using dbimage
« Reply #7 on: November 04, 2015, 08:51:54 am »
Thanks rvk, It works now.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

mariodosreis

  • New Member
  • *
  • Posts: 16
Re: error in debug when using dbimage
« Reply #8 on: June 22, 2022, 08:45:12 pm »
In the above example you are talking abaout FastReport. I beleave the issue here is Zeos caouse it doesn't perform like SqlDb or IBX
So this is kind of complicated. With all respect this solution is some kind of a patch, not something done from the begining like TEdbImage
that work's no mater what I already tried to convert it from delphi to lazarus but i fail!

 

TinyPortal © 2005-2018