Solved

Saving an image in an InterBase fields using TImage and savetostream (and loading)

Posted on 2004-07-30
6
797 Views
Last Modified: 2008-01-09
i have a blob field in an interbase database in which i would like to store an image from a TImage component (bmp, jpeg, doesn't matter as long as i can do it). It must be done with a stream. I also need to know how to load it from the database.
0
Comment
Question by:kilocoder
  • 2
6 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 11679161
I know a way to store an image into a BLOB field of a Table into a InterBase database, but without using a stream ....
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 43 total points
ID: 11679288
About visualization of the saved image, you should use a TDBImage component via TDataSource to the TIBQuery or TIBTable or TIBDataSet ....
0
 
LVL 6

Assisted Solution

by:gwalkeriq
gwalkeriq earned 41 total points
ID: 11681129

var
  MyBlobFld: TBlobField;
  BlobStream: TMemoryStream;


MyBlobFld := tbl.FieldByName('BlobColumn') as TBlobField;
BlobStream := TMemoryStream.Create(

// assuming you have exec'd query to load data
// qry.Active := true, etc.
MyBlobFld.SaveToStream(BlobStream);

// assuming you have data you want to save
MyBlobFld.LoadFromStream(BlobStream);
// qry.Post, etc.
0
 
LVL 12

Assisted Solution

by:Ivanov_G
Ivanov_G earned 41 total points
ID: 11763089
use the component I wrote for this purpose. You just have to use it instead of regular TImage and set its DataSource and DataSet properties...

// -----------------------------------------------------------

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
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…

708 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