?
Solved

Loading JPeg Images into Oracle 8 Blob Field

Posted on 2000-03-27
2
Medium Priority
?
393 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:hmsills
2 Comments
 
LVL 4

Expert Comment

by:Radler
ID: 2661055
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.
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 300 total points
ID: 2663476
hi hmsills,

just set the blobsize-parameter in the bde-alias to a higher value (default = 32K, the images are stored correctly, but not fully retrieved)

a sample, which shows how could it simpler done

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBTables, StdCtrls, ExtCtrls, jpeg, Grids, DBGrids;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    Query1: TQuery;
    Query2: TQuery;
    OpenDialog1: TOpenDialog;
    Edit1: TEdit;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure Button1Click(Sender: TObject);
    procedure Query2AfterScroll(DataSet: TDataSet);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


(*
//Table definition
CREATE TABLE blob_test
 (
  id                         NUMBER(9),
  image                      BLOB
 )
*)

//Store Image
//Query 1 Holds
{
insert into blob_test (id, image) values
(:id,empty_blob()) returning image into :image
}
procedure TForm1.Button1Click(Sender: TObject);
var JP : TJpegImage;
begin
  If OpenDialog1.Execute then
  begin
    query1.ParamByName('Image').LoadFromFile(Opendialog1.FileName,ftOraBlob);
    query1.ParamByName('Id').AsInteger := StrToInt(Edit1.text); //temporary solution
    Try
      query1.ExecSQL;
      JP := TJpegImage.Create;
      try
        JP.LoadFromFile(Opendialog1.FileName);
        Image1.Picture.Bitmap.Assign(JP);
      finally
        JP.Free;
      end;
    except;
    end;
  end;
end;

//get Image
//query2 Holds - is not live
{
Select * from blob_test
}
procedure TForm1.Query2AfterScroll(DataSet: TDataSet);
var
  BS : TBlobStream;
  JP : TJPegImage;
begin
  If not TBlobField(Query2.FieldByName('IMAGE')).IsNull then
  begin
    BS := TBlobStream.Create(TBlobField(Query2.FieldByName('Image')),bmRead);
    JP := TJPegImage.Create;
    try
      JP.LoadFromStream(BS);
      Image1.Picture.Bitmap.Assign(JP);
    finally
      BS.Free;
      JP.Free;
    end;
  end
  else
    Image1.Picture.Bitmap.Assign(Nil);
end;

//query2 Refresh
procedure TForm1.Button2Click(Sender: TObject);
begin
  query2.Close;
  query2.Open;
end;

end.

hope this helps

meikl
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 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