Link to home
Start Free TrialLog in
Avatar of cc042297
cc042297

asked on

JPG to Paradox

Hi,

Can somebody tell me how to store the content of a JPG photo to a graphic field in a Paradox table? (I understand that Paradox cannot store jpg graphics.)

Regards, CC.
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

Hello

  I made a sample for that, give me your email and i will send it to you, or send me email at nasman@mogaza.org

Avatar of MrGhost
MrGhost

You can load file into a stream or just use TFileStream
Set table field type to BLOB and load the stream into a blob something like this:
function tinsert.SaveToBlob(const Stream: TStream; const AField: TField): boolean;
var
  FieldStr: string;
  PFieldStr: PChar;
begin
  Result := false;
  if (Assigned(AField)) and (Assigned(Stream)) then begin
    try
      Stream.Seek(0,0);
      SetLength(FieldStr, Stream.Size);
      PFieldStr := PChar(FieldStr);
      Stream.Read(PFieldStr^, Stream.Size);
      AField.Value := FieldStr;
      Result := true;
    except
    end;
  end;
end;
Avatar of cc042297

ASKER

Hi MNasman & MrGhost!

Thanks for helping me!

MNasman:
I've sent you an e-mail.

MrGhost:
Do you think that a DBImage can display the photo if I store it in a BLOB instead of a graphic field? I can't see from your code, how it connencts to the jpg file.
Hi MNasman & MrGhost!

Thanks for helping me!

MNasman:
I've sent you an e-mail.

MrGhost:
Do you think that a DBImage can display the photo if I store it in a BLOB instead of a graphic field? I can't see from your code, how it connencts to the jpg file.

Regards, CC.
Hi MNasman & MrGhost!

Thanks for helping me!

MNasman:
I've sent you an e-mail.

MrGhost:
Do you think that a DBImage can display the photo if I store it in a BLOB instead of a graphic field? I can't see from your code, how it connencts to the jpg file.

Regards, CC.
I think so
ok i just try to use this:

procedure TForm1.Button2Click(Sender: TObject);
var
bla:TBLobField;
begin
table1.Insert;
bla := TBlobField(table1.FieldByName('Image'));
bla.LoadFromFile('c:\bla.bmp');
table1.Post;
end;

it works ok with BMP but fails with JPG, so i guess there is some other options for which i dont know or you can convert JPG to BMP!
MrGhost

I have tried the same with the same result.
TImage.LoadFromFile reads jpg without any problem. Delphi help suggests assigning its value to a Field. This is what I did:

Image1.Picture.LoadFromFile(Picture_Dir + Picture_ID + '.jpg');

TGraphicField(T1.FieldByName('Picture')).Assign(Image1.Picture);


Though TImage can read JPG, this far I was not able to find any way to get the picture out of it. :(

TDBImage also has a LoadFromFile method, but it does not read JPG.  I tried to seek for help at EE after trying all these. I did not want to beleave, that Delphi is really missing JPG converting. :))

Regards, CC.

 
ASKER CERTIFIED SOLUTION
Avatar of MrGhost
MrGhost

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
MrGhost

I have tried the same with the same result.
TImage.LoadFromFile reads jpg without any problem. Delphi help suggests assigning its value to a Field. This is what I did:

Image1.Picture.LoadFromFile(Picture_Dir + Picture_ID + '.jpg');

TGraphicField(T1.FieldByName('Picture')).Assign(Image1.Picture);


Though TImage can read JPG, this far I was not able to find any way to get the picture out of it. :(

TDBImage also has a LoadFromFile method, but it does not read JPG.  I tried to seek for help at EE after trying all these. I did not want to beleave, that Delphi is really missing JPG converting. :))

Regards, CC.

 
hi,
an older sample using graphicEX from www.lischke-online.de
(enables many kinds of pictureformats to display)

for storing in the paradox-table, the blobfield should be of binary type,
this sample stores any file into the blobfield

unit db_picture_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,
  ExtDlgs, Db, DBTables, ExtCtrls, DBCtrls, StdCtrls, Mask, ComCtrls,
  GraphicEx, Graphics;

type
  TForm1 = class(TForm)  //db-navigation
    Table1: TTable;              //the table, where the images are stored
    DataSource1: TDataSource;
    Panel1: TPanel;
    ProgressBar1: TProgressBar;
    DBNavigator1: TDBNavigator;
    Button1: TButton;
    Panel2: TPanel;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    Label1: TLabel;
    Label2: TLabel;
    Table2: TTable;
    ScrollBox1: TScrollBox;
    Image1: TImage;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    procedure Button1Click(Sender: TObject); //execute load and preStore to the Blobfield
    procedure FormCreate(Sender: TObject);   //TableOpen
    procedure FormDestroy(Sender: TObject);  //TableClose
    procedure Table1AfterScroll(DataSet: TDataSet);
    procedure Table1BeforeScroll(DataSet: TDataSet);
    procedure Image1Progress(Sender: TObject; Stage: TProgressStage;
      PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
      const Msg: String);
    procedure Table1AfterInsert(DataSet: TDataSet);
    procedure Table1BeforePost(DataSet: TDataSet);
    procedure Button2Click(Sender: TObject);
  private
    Function DisplayImage(AStream : TStream; AExtension : String) : Boolean;
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Function TForm1.DisplayImage(AStream : TStream; AExtension : String) : Boolean;
Var
  //Extended Graphicformats
  AExGraphicClass: TGraphicExGraphicClass;
  AExGraphic: TGraphicExGraphic;
  //Standard Graphicformats
  AGraphicClass : TGraphicClass;
  AGraphic : TGraphic;
  OldPosition : Integer;
begin
  Result := False;  //do as that cannot be loaded
  If Assigned(AStream) then
  try
    OldPosition := AStream.Position;
    try
      AStream.Position := 0;  //Remember Streams Position
      //Check for Extended Graphicformats
      AExGraphicClass := GraphicEx.FileFormatList.GraphicFromContent(AStream);
      if AExGraphicClass <> Nil then
      begin
        AExGraphic := AExGraphicClass.Create;
        try
          //Load Extended Graphicformats
          AExGraphic.LoadFromStream(AStream);
          Image1.Picture.Graphic := AExGraphic;
          Result := True;
        finally
          AExGraphic.Free;
        end;
      end
      else
      begin
        //Check for Standard Graphicformats
        AGraphicClass := GraphicEx.FileFormatList.GraphicFromExtension(AExtension);
        if AGraphicClass <> Nil then
        begin
          AGraphic := AGraphicClass.Create;
          try
            //Load Standard Graphicformats
            AGraphic.LoadFromStream(AStream);
            Image1.Picture.Graphic := AGraphic;
            Result := True;
          finally
            AGraphic.Free;
          end;
        end;
      end;
    finally
      AStream.Position := OldPosition;
    end;
  except
    Raise;
  end;
end;

//Load any file into the BlobField
procedure TForm1.Button1Click(Sender: TObject);
Var
  ExtS : String;  //Holds The Extension
begin
  if (Table1.State = dsEdit) or
     (Table1.State = dsInsert) then    // Do only if in edit mode
  begin
    If opendialog1.Execute then    // If file selected
    begin
      if Table1.FieldByName('Picture').IsBlob then   // this line can deleted
      begin  //remember the Extension
        ExtS := copy(AnsiUpperCase(ExtractFileExt(OpenDialog1.FileName)),2,MaxLongInt);
        try
          TBlobField(Table1.FieldByName('Picture')).LoadFromFile(opendialog1.filename);  //Store the File in the Blob
          Table1.FieldByName('Typ').AsString := ExtS; //Store the Extension in the Grafic-TypField
          try
            //try to load Image, also from file
            image1.Picture.LoadFromFile(opendialog1.filename);
          except
            //failed to load Image, also from file
            showmessage('Cannot display this File as Imageformat'+#10+
                        'File is loaded anyway');
            image1.Picture := Nil;
          end;
        except
          //Any Error
          Raise;
        end;
      end;
    end;
  end else ShowMessage('Table is not in Edit Mode!');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //some prepare
  Opendialog1.Filter :=
    GraphicEx.FileFormatList.GetGraphicFilter([],fstBoth,[foIncludeAll,foIncludeExtension],nil);
  Opendialog1.Options := [ofFileMustExist,ofEnableSizing];
  Table1.Open;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  //some unprepare
  Table1.Close;
end;

//After Scrolling the TableCursor
procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
Var
  AStream : TMemoryStream;
begin
  try
    try
      // Do Only if a Picture available
      if (Table1.FieldByName('Picture').IsBlob) and
         (not(Table1.FieldByName('Picture').IsNull)) then
      begin  //create a stream
        AStream := TMemoryStream.Create;
        try
          //Store the BlobContent into the Stream
          TBlobField(Table1.FieldByName('Picture')).SaveToStream(AStream);
          //Try to Display the image
          if not DisplayImage(AStream,Table1.FieldByName('Typ').AsString) then
          begin
             //Failed to Display the image
            image1.Picture.assign(Nil);
            showmessage('Cannot display this File as Image');
          end;
        finally
          AStream.Free;  // Free Stream
        end;
      end
      else image1.Picture.assign(Nil);  // No Picture saved disable display
    Finally
      //Restore Cursor
      screen.Cursor := crDefault;
      ProgressBar1.Position := 100;
    end;
  except
    //Any Error
    Raise;
  end;
end;

//Prepare progress
procedure TForm1.Table1BeforeScroll(DataSet: TDataSet);
begin
  screen.cursor := crhourglass;
  ProgressBar1.Position := 0;
end;

//some Progress
procedure TForm1.Image1Progress(Sender: TObject; Stage: TProgressStage;
  PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  const Msg: String);
begin
  Progressbar1.Position := Percentdone;
end;


//PreInit index
procedure TForm1.Table1AfterInsert(DataSet: TDataSet);
begin
  Table1.FieldByName('ID').AsFloat := -1;
end;

//Supply index, not a optimal method, but works in local enviroments
procedure TForm1.Table1BeforePost(DataSet: TDataSet);
begin
  If table1.State = dsInsert then
  begin
    table2.open;
    table2.last;
    Table1.FieldByName('ID').AsFloat := Table2.FieldByName('ID').AsFloat + 1;
    Table2.Close;
  end;
end;

//Save any Blobcontent to a file
procedure TForm1.Button2Click(Sender: TObject);
begin
  if (Table1.FieldByName('Picture').IsBlob) and
     (not(Table1.FieldByName('Picture').IsNull)) then
  begin
    SaveDialog1.Filter := '*.'+lowercase(Table1.FieldByName('Typ').AsString);
    SaveDialog1.Filter := SaveDialog1.Filter+'|'+SaveDialog1.Filter;
    SaveDialog1.DefaultExt := lowercase(Table1.FieldByName('Typ').AsString);
    if SaveDialog1.Execute then
    try
      TBlobField(Table1.FieldByName('Picture')).SaveToFile(SaveDialog1.FileName);
    except
      Raise;
    end;
  end
  else Showmessage('Nothing to Restore');
end;

end.

hope this helps
(if needed, then i can send this app with a sample table)

meikl ;-)
and again a more older and more simple sample

//Sample unit for Storing JPG-Images in the JPeg-Format
//in a BlobField in Paradox
//It is recommended, that the BlobField in Paradox is
//Typ Binary, not Graphic, because the file will be stored
//Free for Use
//Appendix: Its a sample, therefore no try, except, finally blocks included
//Created January 1999

unit db_pict_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBTables, ExtCtrls, DBCtrls, StdCtrls, Mask, Menus, jpeg; // JPeg unit is used

type
  TForm1 = class(TForm)
    DBNavigator1: TDBNavigator;
    Table1: TTable;
    DataSource1: TDataSource;
    PopupMenu1: TPopupMenu;     // a Popup linked to Image1
    LoadPicture1: TMenuItem;    // the MenuItem of the Popup
    OpenDialog1: TOpenDialog;
    Image1: TImage;
    DBEdit1: TDBEdit;   // A other DBField
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure LoadJPEG1Click(Sender: TObject);      // File Load and Store in DB
    procedure Table1AfterScroll(DataSet: TDataSet); // Get JPeg and Display in Image1
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  JPegImage : TJpegImage;  //A temporary JPeg

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  JpegImage := TJPegImage.Create; // Create JPeg-Object
  Table1.Open;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Table1.Close;
  JPegImage.Free;  // Free JPeg-Object
end;


//Store
procedure TForm1.LoadJPEG1Click(Sender: TObject);
Var
  MyBlobStream : TBlobStream;      // Streams
  MyJPegStream : TMemoryStream;
begin
  if (Table1.State = dsEdit) or (Table1.State = dsInsert) then    // Do only if in edit mode
  begin
    If opendialog1.Execute then    // If file selected (only JPeg-Files)
    begin
      if Table1.FieldByName('Bild2').IsBlob then   // this line can deleted
      begin
        MyJPegStream := TMemoryStream.Create;      // Handle JPegFile
        JPegImage.LoadFromFile(opendialog1.filename);
        JPegImage.SaveToStream(MyJPegStream);
        // Prepare Blob
        MyBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmReadWrite);
        MyBlobStream.CopyFrom(MyJPegStream,0);  // Copy JPeg into BlobField
        Image1.Picture.Assign(JPegImage);  // Display
        MyJPegStream.Free;  //Free all
        MyBlobStream.Free;
      end;
    end;
  end else ShowMessage('Table is not in Edit Mode!');
end;

//Get
procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
Var
  MyBlobStream : TBlobStream;
begin
  // Do Only if a JPeg available
  if (Table1.FieldByName('Bild2').IsBlob) and (not(Table1.FieldByName('Bild2').IsNull)) then
  begin
    // Prepare Streams
    MyBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmRead);
    JPegImage.LoadFromStream(MyBlobStream);
    Image1.Picture.Assign(JPegImage);
    MyBlobStream.Free;  // Free Stream
  end
  else image1.Picture := Nil;  // No JPeg saved disable display
end;

end.

meikl ;-)

hi again,
just about
>JPG photo to a graphic field
is not possible, except you convert the jpg-file into a bitmap

thats just easy, if needed i could assist with this,
but it would be better to restructure your table,
and let your blobfield to be of binary-type instead of graphic-type,
because the bde-engine stores by a graphic type additional typ-
information in front of the blob-content (4Bytes)

meikl ;-)

Hello CC

I didn't get ur email, anyway here's the sample
the table Structure

PicId : No
PicDesc : Text
Pic : Blob

add image component and link the first two fields with the DBEdit boxes and add image component, cuz DBImage doesn't support the jpeg format
also add open dialog, two buttns and dbgrid


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    DBGrid1: TDBGrid;
    Table1: TTable;
    DataSource1: TDataSource;
    Table1PicID: TFloatField;
    Table1PicDesc: TStringField;
    Table1Pic: TBlobField;
    Label1: TLabel;
    DBEdit1: TDBEdit;
    Label2: TLabel;
    DBEdit2: TDBEdit;
    procedure Image1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
    procedure FormDestroy(Sender: TObject);
  private
    PicPath : string;
    procedure LoadPic;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
uses Jpeg;

procedure TForm1.Image1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    Image1.Picture.LoadFromFile( OpenDialog1.FileName);
    PicPath := OpenDialog1.FileName;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Table1.Append;
  Image1.Enabled := True;
  Image1.Picture.Assign(Nil);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Table1Pic.LoadFromFile(PicPath);
  Table1.Post;
  Image1.Enabled := False;
end;

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
   LoadPic;
end;

procedure TForm1.LoadPic;
begin
  if Table1.State = dsBrowse then
  begin
    Table1Pic.SaveToFile('Tmp.jpg');
    Image1.Picture.LoadFromFile('Tmp.jpg');
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if fileExists('Tmp.jpg') then
    DeleteFile('Tmp.jpg');
end;


give me your email and i will send the project to you

Best regards
Mohammed Nasman
just to remember to read my comments above :-)
I'm always using Blob field to store the images with paradox and other databases :)
? i've nothing other expected
Hi, I really appreciate your help, but I have to leave for a trip now.  I will evaluate your comments as soon I will be back. (12 days)

Best regards, CC.  
Hi Experts,

I'm sorry, but I've forgotten to close this Q.
The code that actually helped me was provided by MrGhost.

Thanx to all of U for your help.

Regards, CC
? the answer just do not answer the initial q,
it just converts a jpg-file to a bitmap-file,
in a complex way

and
this can be done simpler

a bit disapointed


meikl ;-)