Solved

Loading JPeg Images into Oracle 8 Blob Field

Posted on 2000-03-27
2
385 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 75 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error 1.1 400 Bad request idhttp delphi 18 87
Run video youtube webbrowse 10 62
URL for downloading Google Chrome for Win XP 2 155
Firemonkey webbrowser scrollbars ? 1 39
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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