Solved

Loading JPeg Images into Oracle 8 Blob Field

Posted on 2000-03-27
2
387 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
[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
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

Technology Partners: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
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…

749 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