Link to home
Start Free TrialLog in
Avatar of selas
selas

asked on

image doubles, how to fix it?

Then person has no image, it gets previous person picture
My code is:

procedure TForm1.DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid;
  Index: Integer);
begin
Jpeg:=TJpegImage.create;
tmp:=TMemoryStream.create;
try
if not TBlobField(AdoQuery1.FieldByName('nuotrauka')).IsNull then  //is there a picture?
  begin
    TBlobField(AdoQuery1.fieldbyname('nuotrauka')).SaveToStream(tmp);
    tmp.Seek(0, soFromBeginning);
    Jpeg.LoadFromStream(tmp);
    Image1.Picture.Assign(jpeg);
    Image1.visible := true;
  end
    else
  begin
    Image1.Picture.Bitmap.Assign(Nil);  //there is no Image
    Image1.visible := false;
  end;
finally
  jpeg.Free;
  tmp.Free;
end;
end;

How to fix it?
Avatar of kretzschmar
kretzschmar
Flag of Germany image

usual i guess your problem is this line

>Image1.Picture.Bitmap.Assign(Nil);  //there is no Image

use

Image1.Picture.Assign(Nil);  //there is no Image
or
Image1.Picture  := Nil;  //there is no Image

instead

an old solution of mine found at
https://www.experts-exchange.com/questions/10175461/JPG-Images-into-TDBCtrlGrid.html#1384525

was for paradox, but will applyable also for other databases

//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.

hope this helps

meikl ;-)
Avatar of selas
selas

ASKER

i tried:
Image1.Picture.Assign(Nil);  //there is no Image
or
Image1.Picture  := Nil;  //there is no Image

but images still doubles....
hmm,
as far as i remember i run into the same problem (years ago),
but don't know how i solved this, yet.

as far as i remember correct, the dbctrlgrid replicates the controls for each panel,
so the goal should be to find the replicated imagecontrol associated with the
displayed record (which is not the current record)

guess i had a solution with use of a dummy calculated field.

will search in my archives this evening

meikl ;-)
hi,
searched my archives, but it points out that i have not solved this.
i removed the TImage from the tdbctrlgrid, and placed it beside the grid,
to show only the image of the current-record.

sorry, that i have no solution

meikl ;-)
Now, if this is an Access database, I don't see how he's getting the image correctly in the first place.
Access adds and extra header to the image in the blob field and you have to trim the stream to get
the image out.

I will need to create a testbed table but I think I have a solution to this problem.
Now IS this an Access database?
Avatar of selas

ASKER

No i'm using mysql and getting data with AdoQuery
What is the FIELDTYPE for the field that contains the image?

Did you know it is not very efficient to store images in a database?
It is better to store the path to the image in the database and
store the image on disk.
Avatar of selas

ASKER

Field type is blob
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

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
I think with the DBImage having the TDataLink, it causes the DBCtrlGrid panel painting to act differently.
Look how is done in TDBImage.

Before

  if not TBlobField(AdoQuery1.Fiel............

put

  Image1.Picture.Graphic := nil;

Regards
mottor