Solved

How to add Picture to database ms access in Delphi ?

Posted on 2004-04-29
5
682 Views
Last Modified: 2010-04-05
Hi There,
   Do you know how to add picture (JPG, BMP)  in database of MS Access by Delphi ?  Can you tell me any code or some example project ?

  Thank you very much,
   Cristh
0
Comment
Question by:cristh
5 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 250 total points
Comment Utility
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility

   Here is a component I wrote which can read images from the DB and display them in Picture

   ------------------------------------------------------------

unit GIDBImage;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, DB, DBCtrls, JPEG;

type
  TGIDBImage = class(TImage)
  private
    { Private declarations }
    FDataLink        : TFieldDataLink;
    function GetDataField: String;
    function GetDataSource: TDataSource;
    function GetField : TField;
    procedure SetDataField(const Value: String);
    procedure SetDataSource(const Value: TDataSource);
    procedure DataChange (Sender : TObject);
  protected
    { Protected declarations }
    procedure Loaded; override;
    procedure CMGetDataLink (var Message : TMessage); message CM_GETDATALINK;
    procedure Notification (AComponent : TComponent; Operation : TOperation); override;
    procedure LoadPicture; virtual;
  public
    { Public declarations }
    constructor Create (AOwner : TComponent); override;
    destructor  Destroy; override;
    property    Field      : TField         read GetField;
  published
    { Published declarations }
    property DataField     : String         read GetDataField       write SetDataField;
    property DataSource    : TDataSource    read GetDataSource      write SetDataSource;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('GI Data-Aware', [TGIDBImage]);
end;

{ TGIDBImage }

procedure TGIDBImage.CMGetDataLink(var Message: TMessage);
begin
  Message.Result := Integer(FDataLink);
end;

constructor TGIDBImage.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlStyle            := ControlStyle + [csReplicatable];
  FDataLink               := TFieldDataLink.Create;
  FDataLink.Control       := Self;
  FDataLink.OnDataChange  := DataChange;
end;

procedure TGIDBImage.DataChange(Sender: TObject);
begin
  if FDataLink.Field <> nil then
    LoadPicture
  else
    Picture.Graphic := nil;
end;

destructor TGIDBImage.Destroy;
begin
  FDataLink.Free;
  FDataLink := nil;
  inherited Destroy;
end;

function TGIDBImage.GetDataField: String;
begin
  Result := FDataLink.FieldName;
end;

function TGIDBImage.GetDataSource: TDataSource;
begin
  Result := FDataLink.DataSource;
end;

function TGIDBImage.GetField: TField;
begin
  Result := FDataLink.Field;
end;

procedure TGIDBImage.Loaded;
begin
  inherited Loaded;
  if (csDesigning in ComponentState) then DataChange(Self);
end;

procedure TGIDBImage.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (FDataLink <> nil) and (AComponent = DataSource) then
    DataSource := nil;
end;

procedure TGIDBImage.SetDataField(const Value: String);
begin
  FDataLink.FieldName := Value;
end;

procedure TGIDBImage.SetDataSource(const Value: TDataSource);
begin
  FDataLink.DataSource := Value;
  if Value <> nil then Value.FreeNotification(Self);
end;

procedure TGIDBImage.LoadPicture;
var
  IsJPG         : Boolean;
  JPG           : TJpegImage;
  MemStream     : TMemoryStream;
begin
  // load the picture
  // check if the field is BLOB
  if not Field.IsBlob then
    begin
      Picture.Graphic := nil;
      Exit;
    end;
  // determine the type
  with Field as TBlobField do
    begin
      IsJPG := Pos('JFIF', Copy(Value, 1, 10)) > 0;
    end;
  // if it is Bitmap - load it and exit
  if not IsJPG then
    begin
      Picture.Assign(Field);
      Exit;
    end;
  // load the JPG with MemoryStream
  MemStream := TMemoryStream.Create;
  JPG := TJPEGImage.Create;
  try
    TBlobField(Field).SaveToStream(MemStream);
    MemStream.Seek(soFromBeginning, 0);
    // load the JPG and assign it
    JPG.LoadFromStream(MemStream);
    Picture.Assign(JPG);
  finally
    JPG.Free;
    MemStream.Free;
  end;
end;

end.
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility

   Just place the picture on the Form, assing DataSource and DataField properties and Done
0
 
LVL 2

Expert Comment

by:ManiElan
Comment Utility
Look at the URL with step by step Explanations. Ready the summary in the first paragraph before considering.
http://www.efg2.com/Lab/Library/Delphi/ADO/Northwind/index.html
0
 

Author Comment

by:cristh
Comment Utility

 Thank you Ferruccio68.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Delphi OLE Error 8 82
FMX StringGrid1->Canvas->FillRect Problem 3 98
Intraweb download file link ? 1 87
Delphi inherited method 6 36
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

12 Experts available now in Live!

Get 1:1 Help Now