Solved

Loading JPeg Images into Oracle 8 Blob Field

Posted on 2000-03-27
2
383 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now