• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

Assigning a Tjpegimage to a TgraphicField

How does one correctly assign a TjpegImage to a Graphicfield for eg
I get an error when i try the following
jpeg_image.assign( b) //where b is of type TGraphicField(i had given the complete code in my previous question.since i did not get any response i am simplifying the question)
Thanks
venks
0
venks
Asked:
venks
1 Solution
 
venksAuthor Commented:
My question should have been 'How to assign a graphicfield to TjpegImage type'
I am sorry for the mistake in my question.I am trying to get the hang of what is to be assigned to what?
Thanks
venks
0
 
PeterLarsenCommented:
You say 'graphicfield'. Is that a picture loaded from a database or ...?
0
 
geobulCommented:
Hi,

Try this:

type
  TForm1 = class(TForm)
    ADOTable1: TADOTable;
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    procedure LoadJPeg;
    procedure SaveJPeg(filename: string);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses jpeg;

var
JPegImage : TJpegImage;

// loads a jpeg from the current record, field name is 'pic'
// JPegImage has already been created before
procedure TForm1.LoadJPeg;
var
  ms : TMemoryStream;
begin
  // Do Only if a BLOB field available
  if (ADOTable1.FieldByName('pic').IsBlob) and (not (ADOTable1.FieldByName('pic').IsNull)) then begin
    // Prepare the stream
    ms := TMemoryStream.Create;
    try
      TGraphicField(ADOTable1.FieldByName('pic')).SaveToStream(ms);
      ms.Position := 0;
      JPegImage.LoadFromStream(ms);
      Image1.Picture.Assign(JPegImage); // display it on the form
    finally
      ms.Free;  // Free Stream
    end;
  end;
end;

// loads an image from record one
procedure TForm1.Button1Click(Sender: TObject);
begin
  ADOTable1.Open;
  ADOTable1.First;
  JpegImage := TJPegImage.Create;
  try
    LoadJPeg;
  finally
    JpegImage.Free;
  end;
  ADOTable1.Close;
end;

// saves a file in the current record, field name is 'pic'
procedure TForm1.SaveJPeg(filename: string);
var
  ms : TMemoryStream;
begin
  ADOTable1.Edit;
  // Prepare the stream
  ms := TMemoryStream.Create;
  try
    JPegImage.LoadFromFile(filename);
    JPegImage.SaveToStream(ms);
    ms.Position := 0;
    TGraphicField(ADOTable1.FieldByName('pic')).LoadFromStream(ms);
  finally
    ms.Free;  // Free Stream
  end;
  ADOTable1.Post;
end;

// saves an image to record one
procedure TForm1.Button2Click(Sender: TObject);
begin
  ADOTable1.Open;
  ADOTable1.First;
  JpegImage := TJPegImage.Create;
  try
    SaveJPeg('d:\test\pic1.jpg');
  finally
    JpegImage.Free;
  end;
  ADOTable1.Close;
end;

Regards, Geo
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mocartsCommented:
if you want to call like jpeg_image.assign(graphicField) then it wouldn't work until you create new class inhrited from TJpegImage and override Assign method to handle this situation.

if you want assign TGraphicField value to TJpegImage in any possible way then there is example:

procedure AssignGraphicFieldToJpegImage(gf: TGraphicField; ji: TJpegImage);
var
  s: TStream;
begin
  s := gf.DataSet.CreateBlobStream(gf, bmRead);
  try
    ji.LoadFromStream(s);
  finally
    s.Free;
  end;
end;

procedure TForm1.btnLoadImageClick(Sender: TObject);
var
  ji: TJpegImage;
begin
  ji := TJpegImage.Create;
  try
    AssignGraphicFieldToJpegImage(Table1.FieldByName('img'), ji);
    Image1.Picture.Graphic := ji;
  finally
    ji.Free;
  end;
end;

mo.
0
 
CleanupPingCommented:
venks:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
venksAuthor Commented:
thanks
Venkatesh
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now