#1 2012-09-20 13:30:31

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

Save and load PNG image

Hi,

I need save a PNG (with transparent) on a field of my database and the load it to show on my form.

I think I can use the code write for bitmap:

SAVE:

var
  doc: RawByteString;
begin
  SaveAsRawByteString(imgDocument.Picture,doc,gptPNG,80,1500);
  Database.UpdateBlob(TSQLContacts, Contact.ID, 'Document', doc); 
end;

my problem is the code to LOAD the image, now in fact I use:

var
  doc: TSQLRawBlob;
  pic: TBitmap;
begin
   Database.RetrieveBlob(TSQLContacts, Contact.ID, 'Document', doc);
   Contact.Document := doc;

   pic := PictureToBitmap(ContattoRecord.Document);

   if pic <> nil then
     begin
        image.Picture.Bitmap.Assign(pic);
        pic.Free;
     end;
end;

Where PictureToBitmap is:

function PictureToBitmap(const Picture: RawByteString): TBitmap;
var
  ST: THeapMemoryStream;
begin
  Result := nil;
  if Picture='' then
    exit;
  ST := THeapMemoryStream.Create;
  try
    ST.Write(pointer(Picture)^,length(Picture));
    with TSynPicture.Create do
    try
      LoadFromStream(ST);
      result := ToBitmap;
    finally
      Free;
    end;
  finally
    ST.Free;
  end;
end;

Is there a way to get a PNG and not a BMP? In PictureToBitmap there is ToBitmap to get a bitmap, but for other images format?

Offline

#2 2012-09-20 16:36:24

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

Re: Save and load PNG image

Just use something like this in the code:

function PictureToSynPicture(const Picture: RawByteString): TSynPicture;
var
  ST: TStream;
  Pic: TSynPicture;
begin
  Result := nil;
  if Picture='' then
    exit;
  ST := TSynMemoryStream.Create(Picture);
  try
    Pic := TSynPicture.Create;
    try
      LoadFromStream(ST);
      result := Pic;
      Pic := nil;
    finally
      Pic.Free;
    end;
  finally
    ST.Free;
  end;
end;

That is, return the TSynPicture and not its bitmap conversion.

Offline

#3 2012-09-23 12:24:30

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

Re: Save and load PNG image

I have a small problem.
Today I have convert my application from Delphi 2006 to Delphi XE3.
All the code works without problem but I have a small bug, I need to use

initialization
  Gdip.RegisterPictures;

to use the code above (save/load image from/to database) but with this code at runtime all my TImage with PNG image are black, if I comment this code I can see all PNG on my TImage but I cannot save/load image from/to database.

How can I solve it?
Thanks

Offline

#4 2012-10-22 16:25:48

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

Re: Save and load PNG image

Are there other users with the same problem? I cannot use PNG any more...

Offline

#5 2012-10-22 19:25:54

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

Re: Save and load PNG image

PNG are working fine with me, e.g. with SynFile main demo program.

Offline

#6 2012-10-22 20:56:28

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

Re: Save and load PNG image

With Delphi XE3?

Please see the small demo: http://dl.dropbox.com/u/19900180/pngproblem.zip

The code is very simple:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, SynGdiPlus, Vcl.Imaging.pngimage,
  Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

initialization
  Gdip.RegisterPictures;

end.

There is only a form with a PNG, on design time I can see the PNG but on run time I cannot see it (see the exe demo).

I don't understand...

Do you have some ideas?

PS with Delphi 2006 I don't have this problem.

Offline

#7 2012-11-01 22:16:30

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

Re: Save and load PNG image

Can someone test my demo on XE3?

Last edited by array81 (2012-11-01 22:16:40)

Offline

#8 2012-11-06 07:32:56

noobies
Member
Registered: 2011-09-13
Posts: 139

Re: Save and load PNG image

tested on XE2 and 2010 and same problem

Offline

#9 2012-11-06 09:01:49

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

Re: Save and load PNG image

Good, so it's there a bug or a problem with last version of Delphi...
On old version it's works (I have tested only on Delphi 2006).

Offline

#10 2012-11-07 12:01:17

noobies
Member
Registered: 2011-09-13
Posts: 139

Re: Save and load PNG image

if try assign png runtime from file or pngimagecollection i see png
too dont understand why enable Gdip.RegisterPictures; dont show png

Offline

#11 2012-11-07 14:09:15

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

Re: Save and load PNG image

in fact. for the moment I have convert my png to bitmap 32bit so can avoid the problem. I hope this bug will be fixed

Offline

Board footer

Powered by FluxBB