Solved

image doubles, how to fix it?

Posted on 2004-08-26
12
598 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:selas
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_10175461.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 ;-)
0
 

Author Comment

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

but images still doubles....
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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 ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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 ;-)
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
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.
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
Now IS this an Access database?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:selas
Comment Utility
No i'm using mysql and getting data with AdoQuery
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
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.
0
 

Author Comment

by:selas
Comment Utility
Field type is blob
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
Comment Utility
Guys, I was able to get it to work if I used a TDBImage instead of TImage
and did not set the FieldName for the DBImage.

Basically it is the same code as selas posted above but I have reformatted for my testing.

const
  BlobFieldName = 'nuotrauka'

procedure TForm1.DBCtrlGrid1PaintPanel(DBCtrlGrid: TDBCtrlGrid;
  Index: Integer);
var
  JPEG: TJPEGImage;
  tmp: TMemoryStream;
begin
  JPEG := TJPEGImage.Create;
  tmp  := TMemoryStream.Create;
  try
    if not TBlobField(ADOQuery1.FieldByName(BlobFieldName)).IsNull then  //is there a picture?
    begin
      TBlobField(ADOQuery1.Fieldbyname(BlobFieldName)).SaveToStream(tmp);
      tmp.Seek(0, soFromBeginning);
      JPEG.LoadFromStream(tmp);
      DBImage1.Picture.Assign(JPEG);
      DBImage1.visible := true;
    end
      else
    begin
      if DBImage1.Picture <> nil then
      begin
        DBImage1.Picture := nil;
        // To not show the empty image placeholder, uncomment this line.
        //DBImage1.Visible := false;
      end;
    end;
  finally
    JPEG.Free;
    tmp.Free;
  end;
end;
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
I think with the DBImage having the TDataLink, it causes the DBCtrlGrid panel painting to act differently.
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
Look how is done in TDBImage.

Before

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

put

  Image1.Picture.Graphic := nil;

Regards
mottor
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video discusses moving either the default database or any database to a new volume.
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…

771 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

13 Experts available now in Live!

Get 1:1 Help Now