 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
John Fraser Guest
|
Posted: Tue Sep 19, 2006 1:29 pm Post subject: TImage loading from datastream |
|
|
Using D2006 and GraphicEx library.
I can load jpeg, gif's etc into a TImage (using GraphicEx Lib) using
loadfromfile, however I would like to be able to load images from database
BLOB. Has anyone had success with this?
I've tried this;
Graphic := TGraphic.Create;
Stream := TFileStream.Create(FILENAME, fmOpenRead or fmShareDenyWrite);
Graphic.LoadFromStream(Stream);
Stream.Free;
Graphic.Free;
and got an abstract error when trying i got to:
Graphic.LoadFromStream(Stream);
Any ideas?
TIA |
|
| Back to top |
|
 |
Peter Below (TeamB) Guest
|
Posted: Tue Sep 19, 2006 10:38 pm Post subject: Re: TImage loading from datastream |
|
|
John Fraser wrote:
| Quote: | Using D2006 and GraphicEx library.
I can load jpeg, gif's etc into a TImage (using GraphicEx Lib) using
loadfromfile, however I would like to be able to load images from
database BLOB. Has anyone had success with this?
I've tried this;
Graphic := TGraphic.Create;
Stream := TFileStream.Create(FILENAME, fmOpenRead or
fmShareDenyWrite); Graphic.LoadFromStream(Stream);
Stream.Free;
Graphic.Free;
and got an abstract error when trying i got to:
Graphic.LoadFromStream(Stream);
Any ideas?
|
You have to create the proper TGraphics descendent that handles the
image format you are trying to load. TImage.Picture.LoadFromFile
depends on the file extension to figure out which class to create. If
you only have a stream with the image data there is no such clue, so
you have to decide yourself in code which class to create.
--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be |
|
| Back to top |
|
 |
John Fraser Guest
|
Posted: Sat Sep 23, 2006 1:48 pm Post subject: Re: TImage loading from datastream |
|
|
OK, I've got part of the way there, but have hit another snag. This works
for the first 2 time i call it then on the 3rd call I get an error
when assigning/freeing the Graphic object. I've been looking at this and
cant seem to spot the obvious error.
Anyone??
Thanks
John.
procedure TForm1.LoadFormDB ( BlobField : Tfield; FilenameField :
TField);
var
Ext, Filename: string;
NewGraphic: TGraphic;
GraphicClass: TGraphicClass;
Stream : Tstream;
begin
with TMyDBPic(sender) do begin
if (not FilenameField.DataSet.Active) or (FilenameField.AsString = '')
then
exit;
Filename := FilenameField.AsString;
Ext := ExtractFileExt(Filename);
Delete(Ext, 1, 1);
GraphicClass := FileFormatList.GraphicFromExtension(ext);
if GraphicClass = nil then
raise EInvalidGraphic.CreateFmt('UnknownExtension', [Ext]);
NewGraphic := GraphicClass.Create;
try
stream := BlobField.DataSet.CreateBlobStream(BlobField, bmRead);
NewGraphic.LoadFromStream(Stream);
stream.Free;
except
NewGraphic.Free;
raise;
end;
if TMyDBPic(sender).Picture.Graphic <> nil then
Picture.Graphic.Free;
Picture.Graphic := NewGraphic;
end;
end; |
|
| Back to top |
|
 |
Peter Below (TeamB) Guest
|
Posted: Sat Sep 23, 2006 3:07 pm Post subject: Re: TImage loading from datastream |
|
|
John Fraser wrote:
| Quote: | OK, I've got part of the way there, but have hit another snag. This
works for the first 2 time i call it then on the 3rd call I get an
error when assigning/freeing the Graphic object. I've been looking at
this and cant seem to spot the obvious error.
Anyone??
Thanks
John.
procedure TForm1.LoadFormDB ( BlobField : Tfield; FilenameField :
TField); var
Ext, Filename: string;
NewGraphic: TGraphic;
GraphicClass: TGraphicClass;
Stream : Tstream;
begin
with TMyDBPic(sender) do begin
|
What is Sender, what is the purpose of this With statement since you do
not seem to use it anywhere below? Don't use "with", it is just a
subtle source of errors that can be hard to find.
| Quote: |
if (not FilenameField.DataSet.Active) or (FilenameField.AsString =
'') then exit;
Filename := FilenameField.AsString;
Ext := ExtractFileExt(Filename);
Delete(Ext, 1, 1);
GraphicClass := FileFormatList.GraphicFromExtension(ext);
if GraphicClass = nil then
raise EInvalidGraphic.CreateFmt('UnknownExtension', [Ext]);
NewGraphic := GraphicClass.Create;
try
stream := BlobField.DataSet.CreateBlobStream(BlobField, bmRead);
NewGraphic.LoadFromStream(Stream);
stream.Free;
except
NewGraphic.Free;
raise;
end;
|
The load process looks OK so far.
| Quote: |
if TMyDBPic(sender).Picture.Graphic <> nil then
Picture.Graphic.Free;
|
That is the problem. You do not need to free the Picture.Graphic if
your TMyDBPic is a descendent of TImage. In fact if you do it this way
the internal reference of the TPicture to the Tgraphic instance will
not be set to nil! To destroy a picture.graphic assign nil to it. Ah, I
see that the With-ed reference is used here, but inconsistently. Do
yourself a favour and just use a local variable of type TMyDBPic which
you initialize as
DBPic := Sender as TMyDBPic;
instead of the with.
| Quote: | DBPic.Picture.Graphic := NewGraphic;
|
This assignment will destroy the old Graphic, if there is one, and make
a *copy* of NewGraphic. So you have to free the NewGraphic instance
after the assignment, or you have a memory leak.
You could modify the code in a way that eliminates the need for a
NewGraphic variable, by the way. Just do a
DBPic.Picture.Graphic := GraphicClass.Create;
DBPic.Picture.Graphic.LoadFromStream(....);
--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be |
|
| Back to top |
|
 |
John Fraser Guest
|
Posted: Sat Sep 23, 2006 5:05 pm Post subject: Re: TImage loading from datastream |
|
|
Doh,
I cut n pasted from wrong spot; maybe i shouldn't post while jet lagged i
guess.... OK here is what i meant to show;
On the third time it gets called it throws an Einvalidpointer exception.
TIA
procedure TForm1.SpeedButton14Click(Sender: TObject);
var
Ext, Filename: string;
NewGraphic: TGraphic;
GraphicClass: TGraphicClass;
Stream : Tstream;
BlobField : Tfield;
FilenameField : TField;
begin
BlobField := AttachATTACHMENT;
FilenameField := AttachFILENAME;
if (not FilenameField.DataSet.Active) or (FilenameField.AsString = '')
then
exit;
Filename := FilenameField.AsString;
Ext := ExtractFileExt(Filename);
Delete(Ext, 1, 1);
GraphicClass := FileFormatList.GraphicFromExtension(ext);
if GraphicClass = nil then
raise EInvalidGraphic.CreateFmt('UnknownExtension', [Ext]);
NewGraphic := GraphicClass.Create;
try
stream := BlobField.DataSet.CreateBlobStream(BlobField, bmRead);
NewGraphic.LoadFromStream(Stream);
stream.Free;
except
NewGraphic.Free;
raise;
end;
if Image1.Picture.Graphic <> nil then
Image1.Picture.Graphic.Free;
Image1.Picture.Graphic := NewGraphic;
end; |
|
| Back to top |
|
 |
Dennis Passmore Guest
|
Posted: Sat Sep 23, 2006 7:28 pm Post subject: Re: TImage loading from datastream |
|
|
You could always just use a TBlobStream and check if it has any contents before
trying to load your NewGraphic from nothing.
var
TB: TBlobStream;
begin
TB := TBlobStream.create(BlobField, bmRead);
if TB.Size > 0 then
NewGraphic.LoadFromStream(Stream);
TB.Free; |
|
| Back to top |
|
 |
Peter Below (TeamB) Guest
|
Posted: Sun Sep 24, 2006 3:18 pm Post subject: Re: TImage loading from datastream |
|
|
John Fraser wrote:
| Quote: | Doh,
I cut n pasted from wrong spot; maybe i shouldn't post while jet
lagged i guess.... OK here is what i meant to show; On the third time
it gets called it throws an Einvalidpointer exception.
|
Read the rest of my first reply. By the way, i was wrong with the very
last part, if you do a picture.graphic := graphicclass.create you would
still leak the created instance since the assignment makes a copy. To
avoid having to copy the complete image data on the assignment you have
to do it this way:
NewGraphic := GraphicClass.Create;
try
DBPic.Picture.Graphic := NewGraphic;
finally
NewGraphic.Free;
end;
DBPic.Picture.Graphic.LoadFromStream(Stream);
--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be |
|
| Back to top |
|
 |
Peter Below (TeamB) Guest
|
Posted: Sun Sep 24, 2006 3:18 pm Post subject: Re: TImage loading from datastream |
|
|
Dennis Passmore wrote:
| Quote: | You could always just use a TBlobStream and check if it has any
contents before trying to load your NewGraphic from nothing.
var
TB: TBlobStream;
begin
TB := TBlobStream.create(BlobField, bmRead);
|
Not advisable. The dataset may need a special descendent of TBlobstream
to work properly.
--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|