Solved

image doubles, how to fix it?

Posted on 2004-08-26
12
603 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11900751
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
ID: 11900893
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
ID: 11901051
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 11910027
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
ID: 11976974
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
ID: 11976980
Now IS this an Access database?
0
 

Author Comment

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

Expert Comment

by:EddieShipman
ID: 11996607
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
ID: 12042853
Field type is blob
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 12044945
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
ID: 12044961
I think with the DBImage having the TDataLink, it causes the DBCtrlGrid panel painting to act differently.
0
 
LVL 4

Expert Comment

by:mottor
ID: 12062207
Look how is done in TDBImage.

Before

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

put

  Image1.Picture.Graphic := nil;

Regards
mottor
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi application Soap connection 5 154
Downloading email attachments 2 96
Delphi and Access based Enumeration 9 90
TlistView is Really heavy on Android 3 44
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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