?
Solved

Loading a jpeg image into a TgraphicField

Posted on 2003-03-14
14
Medium Priority
?
2,815 Views
Last Modified: 2013-11-19
I have the following procedure for loading a jpeg image into a blob field
I get the error on the line which is underlined with the error message
" EConverError -can not assign a TgraphicField to a Tjpegimage "
How do  i code so that i do not get any error
procedure TfrPictureView.GetBlob(b: TfrTBlobField);
var
jpeg_image:Tjpegimage;
begin
  if b.IsNull then
    Picture.Assign(nil)
  else
  begin
{$IFDEF IBO}
   b.AssignTo(Picture)
{$ELSE}
   if is_jpeg(b) then //is_jpeg(b)  function just checks whether the image is jpeg type
   begin
           jpeg_image:=Tjpegimage.create;

     try
       jpeg_image.assign((b) );//this line shows error
       -----------------------
//     ( EConverError -can not assign a Tgraphicfield to a Tjpegimage )
   
           except
               on E: EConvertError do
                    ShowMessage(E.ClassName + ' ' + E.Message);
                end;

           picture.Assign(jpeg_image);
           jpeg_image.Free;
   end
   else
        picture.assign(b);
{$ENDIF}
  end;
end;
************************************************************************

I have
0
Comment
Question by:venks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 9

Expert Comment

by:mocarts
ID: 8139092
you should use stream to load image.

var
..
s: TStream;
begin
...
s := b.DataSet.CreateBlobStream(b, bmRead);
try
  ..
  jpeg_image := TJPEGImage.Create;
  jpeg_image.LoadFromStream(s);
finally
  s.Free;
end;
..
end;

take a look at PAQ:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20532935.html

wbr, mo.
0
 
LVL 3

Expert Comment

by:BTecho
ID: 8140364
Hi venks.

What database are you using? If you're using Paradox AFAIK the Graphics or Picture type field does not support Jpeg, but only Bmps. Try using a Binary field instead.

procedure tform1.SaveBlob;
begin
    Table1.Append;
    (Table1.FieldByName('MyBlobField') as
TBlobfield).LoadFromFile(OpenPictureDialog1.Filename);
    Table1.Post;
end;

procedure TForm1.LoadBlob;
var
jpg:TJpegImage;
s:TBlobStream;

begin

jpg:=TJpegImage.Create;

s:=TBlobStream.Create(TBlobField(Table1.FieldByName(MyBlobField')),bmread);
jpg.LoadFromStream(s);
Image1.Picture.Graphic.Assign(jpg);

jpg.free;

end;

0
 
LVL 1

Author Comment

by:venks
ID: 8141257
Actually the code is not mine.It is the code taken from Freereport component as modified by jay Dubal availble from http://groups.yahoo.com/group/freereport-en/ (where jdubal has attachec an attachment).I am able to load other types of graphics but not jpeg.He claimed that he is able to load jpeg too.
Free report is an excellent component otherwise for dynamic reporting except for this problem
If any one has  found a solution i will be glad to award more points so that more people can use the component for loading jpeg images.
I am using binary & not graphic field type
thanks
venks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:venks
ID: 8141350
Actually the code is not mine.It is the code taken from Freereport component as modified by jay Dubal availble from http://groups.yahoo.com/group/freereport-en/ (where jdubal has attachec an attachment).I am able to load other types of graphics but not jpeg.He claimed that he is able to load jpeg too.
Free report is an excellent component otherwise for dynamic reporting except for this problem
If any one has  found a solution i will be glad to award more points so that more people can use the component for loading jpeg images.
I am using binary & not graphic field type
thanks
venks
0
 
LVL 1

Author Comment

by:venks
ID: 8141911
The problem is how to assign a jpeg image to a TField type
I want code for that & not how to load a jpeg image
thanks
venks
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8143400
code in your intial q is how to load image _from blob_
if and error you noted is raised because you assign to TField TGraphic object which aren't compatible (don't  know how to assign to each other).
if you need to save to field image youse same CreateBlobStream method of TDataset:

..
s: TStream;
begin
...
s := b.DataSet.CreateBlobStream(b, bmWrite);
try
 ..
 jpeg_image.WriteToStream(s);
finally
 s.Free;
end;
..
end;

wbr, mo.
0
 
LVL 1

Author Comment

by:venks
ID: 8189209
I want code to assign a graphicfield to a jpegImage.Is it possible or not.I have given the error raised in my question though i wrongly framed the question.I was quite confused and hence have not framed the question correctly.
Please see the initial question & error message i have
given which is reproduced below
(" EConverError -can not assign a TgraphicField to a Tjpegimage ")
venks
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8189494
From FreeReport help. "You also can use JPEG images, if correcting FR.inc file ($DEFINE JPEG)."
Did you defined?
and you can try to download newest version of FreeReport (currently 2.32-1).

and..
  jpeg_image := TJpegImage.Create;
  try
    jpeg_image.Assign(b);
    Picture.Assign(jpeg_image);
  finally
    jpeg_image.Free;
  end;

..works fine for me (Delhpi7)
you can try also:

type TGetProtected = class(TPersistent) end;
procedure GetBlob...
begin
...
  jpeg_image := TJpegImage.Create;
  try
    TGetProtected(b).AssignTo(jpeg_image);
    Picture.Assign(jpeg_image);
  finally
    jpeg_image.Free;
  end;
..
end;

wbr, mo.
0
 
LVL 1

Author Comment

by:venks
ID: 8193120
DEar Mocarts
Thanks for the tip.It does not work for me with delphi 5 & delphi 6.I have just got delphi 7.I will try it out
Actually the error comes in
jpeg_image.Assign(b);
(" EConverError -can not assign a TgraphicField to a Tjpegimage ")
I dont know why the error comes for me when it is working fine for you.
I have already uncommented FR.inc file ($DEFINE JPEG)."
I am able to load jpeg files by using Load from the Edit menu of the Picture compo.
Only i am not able to load a jpeg image from a database.
C an you send your fr_class.pas & any demp that you may have created for me to try it out
Thanks
venks

0
 
LVL 9

Accepted Solution

by:
mocarts earned 1000 total points
ID: 8193483
there is intermediate class TStreamedPersistent and method AssignTo introduced in Delhpi 7 - thats why code works for me. In d5 and d6 you must use intermediate stream to load jpeg from blob field :(

procedure TfrPictureView.GetBlob(b: TfrTBlobField);
var
  jpeg_image: TJpegImage;
  s: TStream;
begin
...
 if is_jpeg(b) then begin
  jpeg_image := TJpegImage.Create;
  try
    // create stream to get image
    s := b.DataSet.CreateBlobStream(b, bmRead);
    try
      // load image from stream
      jpeg_image.LoadFromStream(s);
    finally
      // must free
      s.Free;
    end;
    // assigning loaded image to picture
    Image1.Picture.Assign(jpeg_image);
  finally
    // must free
    jpeg_image.free;
  end;
 end;  // if is_jpeg(b) then
...
end;

this will be my last comment as I starting to repeat :)
D5 and up working example you can download from http://www.grava.lv/files/pro/frjpg.zip
there is only one unit1 in archive (without project) - add it to new project to run.

wbr, mo.
0
 
LVL 1

Author Comment

by:venks
ID: 8230082
Thanks for all the help though i could not download the file from www.grava.lv
venks
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8231214
it seams that server is down today.. i will give you a message when it will be back on the line.
mo.
0
 
LVL 1

Author Comment

by:venks
ID: 8247132
Mo
I am still unable to download.why dont you send it to
venks@vsnl.com
venks
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8305707
it seems that server is now online - you can download that file. (i hadn't local copy of file so I can't send to you..)
note: this file doesn't contain example with FastReport - just how to save and load image from dataset (in my example ClientDataSet).

wbr, mo.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question