Link to home
Start Free TrialLog in
Avatar of hmsills
hmsills

asked on

Loading JPeg Images into Oracle 8 Blob Field

I am loading a jpeg file into an Oracle8 blob field using a TJpegImage and Tmemorystream object.  Here is the code:
 procedure TFrGraphic.Button1Click(Sender: TObject);
                      var
                         jp : TJpegImage;
                         st : TMemoryStream;
                         name, path : string;
                      begin
                         //exit;

                         path := txtfilepath.text;
                         name := txtfilename.text;
                         st := TMemoryStream.Create;
                         try
                           jp := TJpegImage.Create;
                           try
                             if true then
                             begin
                             jp.LoadFromFile(path+name);
                             jp.SaveToStream(st);
                             st.Seek(0,soFromBeginning);
                                  {qmax.open;
                                  pict_no := qMaxNum.AsInteger + 1;
                                  qmax.close;
                                  dmglobal.database1.StartTransaction;
                                  dbtImages_.Insert; }
                                  qryGraphicGRAPHIC.LoadFromStream(st);
                                  {dbtImages_Pict_No.AsInteger := pict_no;
                                  dbtImages_Pict_File.AsString := cpn; }
                                  if reloading = true then
                                  begin
                                  reloading := false;
                                  getimage := strtoint(txtdimagenum.text);
                                  end;
                                  qryGraphic.Post;
                             //     dmglobal.database1.commit;
                                  btnreload.Enabled := true;
                             end;

                           finally
                                  jp.Free;
                           end;
                         finally
                                st.Free;
                         end;
                      txtfilepath.text := '';
                      txtfilename.text := '';
                      pnlLoad.visible := False;
                      end;

                      This appears to work fine.  I am then displaying the image via a TImage object (with the stretch property set to true)  using the following code:

procedure TFrGraphic.ShowPicture;
                      var
                         jp : TJpegImage;
                         st : TMemoryStream;
                      begin

                         st := TMemoryStream.Create;
                         try
                           jp := TJpegImage.Create;
                           try
                                  FrGraphic.qryGraphicGRAPHIC.SaveToStream(st);
                             st.Seek(0,soFromBeginning);
                             jp.LoadFromStream(st);
                             if (jp.Width = 0) or (jp.height = 0) then
                             begin
                             FrGraphic.Image1.Picture.Graphic := nil;
                             Application.Processmessages;
                             end
                             else
                           //  FrGraphic.Image1.Visible := False;
                             begin
                             Application.Processmessages;
                             FrGraphic.Image1.Picture.Graphic := jp;
                             DisplayPicture;
                             btnreload.Enabled := true;
                             end;
                         //    PictArea.Caption := '';
                           finally
                                  jp.Free;
                           end;
                         finally
                                st.Free;
                         end;
                      end;

This also appears to work except occasionally.  In 2 out of 10 files when they are displayed, the first 20 bytes or so seam to be fine but then the
image appears to shift to the left and partially wrap around the right side.

 Am I missing something?  If I take the .jpg file and make a small adjustment to it (change the size of the image smaller or larger) and then reload the graphic into the database, it appears to be fine.   These images are scanned black and white drawings.

 Any advise would be appreciated.
Avatar of Radler
Radler

Hi hmsills,

try this
                        st := TMemoryStream.Create;
                                            try
                                              jp := TJpegImage.Create;
                                              try
                                                     FrGraphic.qryGraphicGRAPHIC.SaveToStream(st); st.Seek(0,soFromBeginning);
jp.LoadFromStream(st);
if (jp.Width = 0) or (jp.height = 0) then
begin
//FrGraphic.Image1.Picture.Graphic := nil; Avoid this use a blank image
Application.Processmessages; //Why ???
end
else
//  FrGraphic.Image1.Visible := False;
begin
Application.Processmessages; //Why ??
FrGraphic.Image1.Picture.Graphic.Assign( jp );
DisplayPicture;
btnreload.Enabled := true;
end;
//    PictArea.Caption := '';
finally
jp.Free; //Remenber the assignment above ?
end;
finally
st.Free;
end;
end;

Good luck.

T++, Radler.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial