Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load .jpg into DBImage ??

Status
Not open for further replies.

Delphard

Programmer
Jul 22, 2004
144
RS
Can I load .jpg (and/or .gif) pictures into DBImage component and how?
 
Are you trying to load it from a database or do you want to simply assign and display a jpg or gif stored on disk? Please give us more information on what you're trying to do.
If you are not extracting the image from a database then consider using TImage.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Yes you can. I struggled with it last week, but it is possible.

The dbImage component works great with bmp-files, but jpeg is a little more complicated.

Steps:

1) Prepare your database, I have used Interbase with a Blob field sub type 0 for this
IMAGES BLOB SUB_TYPE 0 SEGMENT SIZE 80,

2)Upload the file to the database. I have done this using a combination of a clipboard, OpenPictureDialog, dbImage and JPegImage.

Code:
procedure TForm1.btnLoadClick(Sender: TObject);
var
   SPicFileName: string;
   JP : TJPegImage;
   C : TClipboard;
begin
  if query1Images.IsNull and OpenPictureDialog1.Execute then
  begin
    JP := TJPegImage.Create;
    C:= TClipboard.Create;
    SPicFileName:=  OpenPictureDialog1.FileName;
    try
      JP.LoadFromFile(SpicFileName);
      C.Assign(JP);
      DBImage1.PasteFromClipboard;
      query1.Post;
    finally
    JP.Free;
    C.Free;
  end;
  end; //if
    
end;

The problem is that the contents of the dbImage are not 100% visible, especially if you work with different size images. For zooming in I use a TImage component to copy the contents of the blobfield

Code:
procedure TForm1.btnShowClick(Sender: TObject);
begin
 Image1.Picture.Assign(Query1Images);
end;

I have received some BDE errors, but nothing that crashes the program or inhibits the insertion of records.

Steven van Els
SAvanEls@cq-link.sr
 
for Stretchwickster:
I'm using Firebird 1.0.3 table, with BLOB field for picture (maybe I should set some specific SubType for BLOB field if want to use it for pics?)
In Fields Editor, I set "ftGraphic" for BlobType Property of my pic/BLOB field, but results are same as when this Property is "ftBlob" : just .bmp pics
I can load into this BLOB field. This is code used to Load pic into BLOB:

procedure TForm17.BitBtn1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
Unit2.DataModule2.IBTable6.Edit;
Unit2.DataModule2.IBTable6SLIKA.LoadFromFile(OpenPictureDialog1.FileName); // SLIKA is that BLOB field
Unit2.DataModule2.IBTable6.Post;
end;
end;
---------------------------------------------

for aaronjme:
I try that improved(?!) components but none of them doesn't succeed to put .jpeg into Firebirds BLOB field

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


for svanels:
Thanks for advice/code, but when I try your code:
"Cannot assign TJPEGImage to a TClipboard..."
even with QClipbrd and Jpeg units included in uses section :(
 
I'm only fairly new to Delphi, but if you can work with bitmaps why not load the jpeg and draw the jpeg to the bitmap (see below)? I know it's not elegant, but whatever.

Code:
bmp := TBitmap.Create;
try
  jpg := TJPEGImage.Create;
  try
    jpg.LoadFromFile(FilePath);
    Bmp.Width := jpg.Width;
    Bmp.Height := jpg.Height;
    Bmp.Canvas.Draw(0, 0, jpg);
    { Plus whatever else you need to do with the bitmap }
  finally
    jpg.free;
  end;
finally
  bmp.Free;
end;
 
QClipbrd?

In the uses clause after implementation I have only:

uses jpeg, Clipbrd;

Does the construction
Code:
JP.LoadFromFile(SpicFileName);
 C.Assign(JP);

doesn't work? Trying to load the file directly to the clipboard will fail.

Another thing, the blob sub type should be 0 for displaying graphic fields. I assume that interbase and firebird have the same origin and structure.

Also another thing, leave the property of the field to ftBlob.

In design time when executing the code, the cpu window will eventually pop-up, but when running the executable file only a message will appear "operation not supported" and it will post the image to the database.

Trying to load a picture to an already occupied blob field will result in an error. A work around is to empty the blob field first.

For your convenience here is the entire code:

Code:
unit gtest;

interface

uses
  Windows, Messages, SysUtils,  Classes, Graphics, Controls, Forms,
  Dialogs,  ExtCtrls, StdCtrls, ExtDlgs, DBCtrls, DB,
  Grids, DBGrids, DBTables;

type
  TForm1 = class(TForm)
    OpenPictureDialog1: TOpenPictureDialog;
    btnLoad: TButton;
    Database1: TDatabase;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Image1: TImage;
    DBNavigator1: TDBNavigator;
    DBImage1: TDBImage;
    Query1: TQuery;
    Query1NAME: TStringField;
    Query1IMAGES: TBlobField;
    Query1DESCRIPT: TStringField;
    btnShow: TButton;
    btnReload: TButton;
    btnClear: TButton;
    procedure btnLoadClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnShowClick(Sender: TObject);
    procedure btnReloadClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;



var
  Form1: TForm1;

implementation
  uses jpeg, Clipbrd;
{$R *.dfm}



procedure TForm1.btnLoadClick(Sender: TObject);
var
   SPicFileName: string;
   JP : TJPegImage;
   C : TClipboard;
begin
  if query1Images.IsNull and OpenPictureDialog1.Execute then
  begin
    JP := TJPegImage.Create;
    C:= TClipboard.Create;
    SPicFileName:=  OpenPictureDialog1.FileName;
    try
      JP.LoadFromFile(SpicFileName);
      C.Assign(JP);
      DBImage1.PasteFromClipboard;
      query1.Post;
    finally
    JP.Free;
    C.Free;
  end;
  end; //if
    
end;




procedure TForm1.FormCreate(Sender: TObject);
begin
  database1.Connected := true;
  query1.Active := true;
end;

procedure TForm1.btnShowClick(Sender: TObject);
begin
 Image1.Picture.Assign(Query1Images);
end;

procedure TForm1.btnReloadClick(Sender: TObject);
begin
  query1.Close;
  query1.Open;
end;

procedure TForm1.btnClearClick(Sender: TObject);
begin
  query1.Edit;
  query1images.Clear;
  query1.Post;
end;

end.

Regards


Steven van Els
SAvanEls@cq-link.sr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top