Link to home
Start Free TrialLog in
Avatar of Felixin
Felixin

asked on

JPG Images into TDBCtrlGrid

Hi everyone.

My objetive is to load Blob fields containing either BitMap or Jpeg (or TIFF, or GIF,...) in something similar to a TDBCtrlGrid.

I have tried some methods but:

1.- TDBImage does not load Jpg/Jpeg images,
2.- When I try the DBCtrlGrid.OnPaintPanel to upload the images manually, I get bizarre results, making me think that I'm not accesing the correct record of the associated DataSet.

Does anyone know how to make it, out of making an array of images and loading it one by one?

Thanks

Felixin
ASKER CERTIFIED SOLUTION
Avatar of ZifNab
ZifNab

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
Avatar of kretzschmar
hi Felixin,

there is someone faster (zif you are it)
this should do it

//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
//Expanded June 1999 with a DBCtrlGrid

unit db_pict_u;

interface

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

type
  TForm1 = class(TForm)   // A other DBField
    DBNavigator1: TDBNavigator;
    Table1: TTable;
    DataSource1: TDataSource;
    PopupMenu1: TPopupMenu;     // a Popup linked to Image1
    LoadPicture1: TMenuItem;    // the MenuItem of the Popup
    OpenDialog1: TOpenDialog;
    DBCtrlGrid1: TDBCtrlGrid;
    Image1: TImage;
    DBEdit1: TDBEdit;
    Table2: TTable;             // Visual Output for the JPeg
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure LoadJPEG1Click(Sender: TObject);      // File Load and Store in DB
    procedure Table1AfterScroll(DataSet: TDataSet);
    procedure DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid;
      Index: Integer); // 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;


procedure TForm1.DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid;
  Index: Integer);
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
appendix,

my sample above is for jpeg only, if you want to store mutliple formats, you must determine with an additional field, which format is stored and of course depending of this information you have another get- and storing routine.

meikl
Avatar of Felixin
Felixin

ASKER

Hi Zifnab,

I'm checking your answer.

Anyway, thanks for being so fast.

I'll be answering you soon.

Felixin
Avatar of Felixin

ASKER

ZifNab,

The code that you sent me was, most of all, equal to what I wrote before asking this question.

What was wrong with it was the allign property of the TImage in my code!:
When testing your code, I've just put a TImage on the panel, without setting the Allign property to alClient, as it was in my project .... and it worked. Then I thought that the diference was just this, and tried changing the align property to alClient...AND IT STOPPED WORKING!: only the first image was repeated on each panel, as it was doing on my project.

Very bizarre!.

Anyway, yours was the answer, yours are the points.

I have to be fair.

Thanks.

Felixin
? zif, has you sent code ?
No, I don't understand anything about it, I only referenced to the components!
Felexin, can you clearefy? Are you using kretzschmar's example or one of the components I've proposed?
Avatar of Felixin

ASKER

I'm sorry, I made a mistake.

I'm using kretzschmar code example.

I'm really sorry. Tell me what can I do, and I'll make it.

Felixin
hi felixin,

it doesn't matter, you have nothing to do.
i was just wondering. glad that is now working for you.

meikl
Avatar of Felixin

ASKER

Well,

Thank you anyway

Felixin
kretzschmar, I can give you the same amount of points, that's fair I guess. Only problem then is still that I've recieved some points.

Felixin, how much points were on this q'n?
hi zif,

let it be, points are not all of life.
it is more important that felixin can go on.

but, thanks for the offer

meikl ;-)
Avatar of Felixin

ASKER

Boys,

I'm gonna cry  ;-)).

Points were 150.

Thanks to you two.

Thanks a lot.

Felixin
ok, let it be this time.