#1 2015-03-26 11:03:12

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Load/Save a JPEG image from/into a MemoryStream and save it in a file

Good afternoon!

I have a code like this:

var
  MemStream: TSynMemoryStream;
  JPEGImage: TSynPicture;
begin
  JPEGImage := TJpegImage.Create;

  // DB access to load a JPEG from a database  
  MemStream := TSynMemoryStream.Create(NewStmt.ColumnBlob('JPEGDATA'));
  
  MemStream.Position := 0;
  JPEGImage.LoadFromStream(MemStream);
  JPEGImage.SaveToFile('c:\temp\myimage.jpeg');

  ... 

end;

The problem is an empty file is created after the execution "SaveToFile".

I have debugged this method and saw that TSynLibrary is nil (screenshot 1) and the method "SaveAs(...)" exits before it can save something (screenshot 2).

Screenshot 1: http://s27.postimg.org/5ab8ioilf/gdiplus.jpg
Screenshot 2: http://s27.postimg.org/mp024p543/gdiplus2.jpg


Could you help me? :-)

Offline

#2 2015-03-26 13:41:15

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Load/Save a JPEG image from/into a MemoryStream and save it in a file

I don't have use ColumnBlob until now but the framework don't load the blob by default (to save memory) so you may need to force the blob reading.
TSQLRecord work in this mode.

Offline

#3 2015-03-26 13:44:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,227
Website

Re: Load/Save a JPEG image from/into a MemoryStream and save it in a file

You have to run Gdip.RegisterPictures to initialize the GDI+ library.

Run it in the initialization section of your project.

Offline

#4 2015-03-26 15:13:45

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: Load/Save a JPEG image from/into a MemoryStream and save it in a file

ab wrote:

You have to run Gdip.RegisterPictures to initialize the GDI+ library.

Run it in the initialization section of your project.

I have tried it and I get a message like "EFilerError : A class named TJpegImage already exists".

Do you have any suggestions why!?

Offline

#5 2015-03-26 15:17:01

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: Load/Save a JPEG image from/into a MemoryStream and save it in a file

array81 wrote:

I don't have use ColumnBlob until now but the framework don't load the blob by default (to save memory) so you may need to force the blob reading.
TSQLRecord work in this mode.

I know how the blob fields are handled, the code fetches the blob data, i.e. the picture, and I can load it from the stream object into the image-component. The problem is the TSynPicture or the probmlem is me because I can't use TSynPicture in the right way ;-) :-D

Offline

#6 2015-03-26 15:18:57

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,227
Website

Re: Load/Save a JPEG image from/into a MemoryStream and save it in a file

I see two possibilites:
- Run Gdip.RegisterPictures sooner, e.g. in the initialization section of an unit listed topmost of your uses clause of your .dpr
- Get rid of all reference to the Jpeg.pas unit from your project.

Or, instead of calling Gdip.RegisterPictures, run

Gdip := TGDIPlus.Create('gdiplus.dll');

Offline

Board footer

Powered by FluxBB