Solved

How do I store JPG images in a Paradox database for display in a Delphi Imager

Posted on 2008-06-19
5
679 Views
Last Modified: 2013-11-23
I used Delphi 5 to create software that stores bimap images in a Paradox 7 database and displays the images using the DBImager. This works well but storing a lot of bitmap images creates huge database BLOB files.  Is it possible to store and display the images as either Jgeg or GIF files that require substantially less space than the bitmaps?
Thank you.
Tom Myers
0
Comment
Question by:Tom1myers
5 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 250 total points
ID: 21828093
it is very easy to turn a bitmap into a jpeg or gif
e.g.
  tempjpg := TJpegImage.Create;
  try
    tempjpg.Assign(tempbitmap);

and TJpegImage has a "SaveToStream" method that you can use


If you want to use the "Graphic" type field, then you can still use a Jpeg in it
here's a great example
http://www.efg2.com/Lab/Library/Delphi/Graphics/JohnHerbster_T_Jpeg_1%2019991207.zip
link from this page
http://www.efg2.com/Lab/Library/Delphi/Graphics/FileFormatsAndConversion.htm
in the "BLOB" section, even though it uses a TGraphicField

if you would prefer to use the paradox Blob type, please let me know.
You can still do it very easily, you just need to use the "CreateBlobStream" method of the field instead
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 200 total points
ID: 21828673
hi,
an older sample using graphicEX from Mike Lischke at
http://www.soft-gems.net/index.php?option=com_content&task=view&id=13&Itemid=33
(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

meikl ;-)
0
 
LVL 8

Assisted Solution

by:BdLm
BdLm earned 50 total points
ID: 21829427
here is an EE discussion how to load and store XML docs to a blo field
http://www.experts-exchange.com/Programming/Languages/Pascal/Q_23333986.html,
is the same way you may store image data to the blop field
{**********************************************

 * in::

 *

 *  aTable       [paradox table]

 *  aPicField    [fieldname]

 *  aPicpath     string 'C:\cmyimage.bmp';

 *  support      http://www.experts-exchange.com/Programming/Languages/Pascal/Q_23333927.html

 *

 **********************************************}
 
 

procedure SaveBMPToTable(aTable: TTable; aPicField:TBlobField; aPicPath: string);
 

var

  fS  : TFileStream;

begin

  fs:=TFileStream.Create(aPicPath, fmOpenRead);

  try

   aTable.Edit;

   aPicField.LoadFromStream(fs);

   aTable.Post;

  finally

   fs.Free;

  end;

end;
 

{***********************************************

 *

 *  reverse function of above

 ***********************************************}
 

procedure SaveTableToBMP(aTable: TTable; aPicField:TBlobField; aPicPath: string);

begin
 

  try

     aPicField.SaveToFile(aPicPath);
 

  finally
 

  end;
 

end;

Open in new window

0
 

Author Closing Comment

by:Tom1myers
ID: 31469116
#1. (TheRealLoki) This will work well with my present Paradox Graphic Fields, I appreciate your efforts;
#2. (kretzschmar) This should work well for future software development. Thank You;
#3. (BdLm) Not what I was looking for but will find it useful in the futrue. Thank you;
0
 

Author Comment

by:Tom1myers
ID: 21830429
Many thanks to those who offered help. This should get me up and running.

TheRealLoki, You made the comment:
(if you would prefer to use the paradox Blob type, please let me know. You can still do it very easily, you just need to use the "CreateBlobStream" method of the field instead)
Any additional information concerning the process would be sincerely apprecaiated.

Respectfully
Tom Myers.

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

16 Experts available now in Live!

Get 1:1 Help Now