Solved

How to save the image into the database in Delphi7?

Posted on 2003-12-10
7
4,493 Views
Last Modified: 2007-12-19
Hi ,
  the function I want is to save the picture in Timage object to the sql database, I have tried several ways to do it ,but not successful.

the picture I load from file may be jpg ,bmp or emf,wmf.  
I look up some references , most of them told me to do like this below:
MS:TmemoryStream;
pic:TGraphic.

image1.picture.bitmap.loadfromfile(filename);
//or pic:=Tgraphic.create;
//    pic.loadfromfile(filename)
image1.picture.bitmap.savetostream(ms);
ms.position:=0;
table1.open;
table1.insert; // or append;
TBolbfield(table1.fieldbyname('image')).loadfromstream(ms);
// or table1.fieldbyname('image').assign(pic);
table1.post;
table1.close;
ms.free;
pic.free;

it works if the picture I load fromis the bitmap file,otherwise If the image is loaded from a jpg file, the exception "bitmap is not valid" comes out;

I have also tried to use TGraphic to load the file, it even failed when I  try to load a jpg file in it.

I am so confused now ,pls tell me what to do .My enviroment"
win2k pro, delphi7. sqlserver 2000;


Regards,
Sam
0
Comment
Question by:Sam80
[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
7 Comments
 
LVL 5

Expert Comment

by:snehanshu
ID: 9918522
Sam80,
  TImage does not support JPEG files.
  Use TJpegImage for image1.
  i.e.

Var
  MyImg : TJpegImage;
Begin
...
  MyImg := TJpegImage.Cleate;
  MyImg.LoadFromFile('MyImg.jpg');
  MyImg.SaveToStream(MS);
 
...


*-*-*-*-*

Also, remember to add jpeg in uses .


HTH,
...Shu
0
 
LVL 5

Expert Comment

by:delphized
ID: 9918685
I think it's better:

var
  F:TFileStream;
begin
  F:=TFileStream.create('YourFileNameHere',fmOpenRead);
  //...
  TBolbfield(table1.fieldbyname('image')).loadfromstream(F);
  //...
  F.free;
0
 

Author Comment

by:Sam80
ID: 9919217
Thank you for your suggestion.

I wanna load both jpeg and bitmap file , further more , load the file in the openpicturedialg supported format(emf,wmf), not just a single format and neednt  specify different way to load the different format file . I will try the TFileStream later.    

Thank you all .

Regards,
Sam
0
Independent Software Vendors: 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 17

Expert Comment

by:geobul
ID: 9919818
Hi,

Just add jpeg to your uses clause and keep using TImage. This works fine here.

Regards, Geo
0
 

Author Comment

by:Sam80
ID: 9925882
I have use Tbitmap to load the picture in the TImage. And then use table.fieldbyna,e('image').assign(bmp) to save the img to the database.

but I now doubt there will be a mess data in the database for all the image format is bitmap.

================

To Geo,
  In delphi7 , I dont have to add the jpeg unit and use Tjpegimage as well.  but if i add that unit, delphi report that there has no such unit.

To delphized,
  I have try your method , but it's so strange that I read the jpeg file into a memorystream type object and save it into the field like the way you show to me , but in that step , the exception message "bitmap is not valid " still comes out,in contrast , if i save a bitmap memorystream, it works !! . I doubt it's the database's problem . in my sql, I define the field "image" as image type, length is 16. why it can verify my memorystream is bitmap or not??

To all ,
    The another problem I meet  is the Tgraphic problem. I cannot use Tgraphic.loadfromfile because of the error "abstract error" . I have tried to do :   gra:Tgraphic;
gra:=Tgraphic.create;
gra:=Timage.picture.Graphic; // not gra.assign(..)
in this way, the gra has the picture's width and height same as that in Timage  !

Regards,
Sam
0
 
LVL 5

Expert Comment

by:delphized
ID: 9926479
I told you TFileStream... and I didn't know you were usin a Image field (Ithought a blob field).

You can't use the TGraphic method because TGraphic is a abstract class that is derived in different subclasses for every king of image (bitmab,jpeg, ...).

After having known this last information, you could make this way;

var
  bmp:TBitmap;
begin
  bmp:=TBitmap.create;
  bmp.height:=Image1.picture.height;
  bmp.width:=Image1.picture.width;
  bmp.canvas.draw(0,0,Image1.picture);
 
and then assign the bmp object to your field

  bmp.free;
end;
0
 
LVL 1

Accepted Solution

by:
hederglan earned 20 total points
ID: 9933951
Hi, Sam80!

I had the same problem some weeks ago...but I found a solution that works fine.
Above follows the code to save and to load an image using BlobFields and TImage...
This code has some functions that were designed specificly for JPEG format......when we are working with JPEG format and MicroSoft Databases we have problems: MS DBs add headers to the blob field making the image unreadable (they are readable only by MS DBs)....to read these images we have to eliminate the readers in the reading process...
But if you work with a bmp or any other all you have to do is not use the jpeg's functions....

So, here is the code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, ExtCtrls, StdCtrls, Db, ADODB, Grids, DBCtrls, DBGrids;

type
  TForm1 = class(TForm)
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    btnShowImage: TButton;
    ADOImage: TImage;
    ADOTable1Name: TWideStringField;
    ADOTable1Description: TWideStringField;
    ADOTable1Author: TWideStringField;
    ADOTable1Type: TWideStringField;
    ADOTable1Size: TFloatField;
    ADOTable1Cost: TBCDField;
    ADOTable1DateUpl: TDateTimeField;
    ADOTable1Picture: TBlobField;
    DBGrid1: TDBGrid;
    procedure btnShowImageClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
     { Public declarations }
  end;


const
 JPEGstarts = 'FFD8';
 BMPstarts = '424D';  //BM

var
  Form1: TForm1;

implementation
uses jpeg;
 {$R *.DFM}

function JpegStartsInBlob (PicField:TBlobField):integer;
var
 bS     : TADOBlobStream;
 buffer : Word;
 hx     : string;
begin
 Result := -1;
 bS := TADOBlobStream.Create(PicField, bmRead);
 try
  while (Result = -1) and (bS.Position + 1 < bS.Size) do
  begin
   bS.ReadBuffer(buffer, 1);
   hx:=IntToHex(buffer, 2);
   if hx = 'FF' then begin
     bS.ReadBuffer(buffer, 1);
     hx:=IntToHex(buffer, 2);
     if hx = 'D8' then Result := bS.Position - 2
     else if hx = 'FF' then bS.Position := bS.Position-1;
   end; //if
  end; //while
 finally
  bS.Free
 end;  //try
end;

procedure TForm1.btnShowImageClick(Sender: TObject);
var
  bS  : TADOBlobStream;
  Pic : TJpegImage;
begin
  bS := TADOBlobStream.Create(AdoTable1Picture, bmRead);
  try
    bS.Seek(JpegStartsInBlob(AdoTable1Picture), soFromBeginning);
    Pic:=TJpegImage.Create;
    try
     Pic.LoadFromStream(bS);
     ADOImage.Picture.Graphic:=Pic;
    finally
     Pic.Free;
    end;
  finally
    bS.Free
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
var sDBPath, sCons: string;
begin
//change the sDBPath to point to your database!
sDBPath := 'c:\!Gajba\About\aboutdelphi.mdb';
sCons := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + sDBPath + ';Persist Security Info=False';

ADOTable1.ConnectionString := sCons;
ADOTable1.TableName := 'Applications';
DataSource1.DataSet := ADOTable1;
DBGrid1.DataSource := DataSource1;

ADOTable1.Active:=True;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ADOTable1.Active:=False;
end;


//Extra!! save JPG to table
procedure SaveJpegToTable(Table: TADOTable; PicField:TBlobField; sPicPath: string);
{
Usage:

  SPicFileName := 'C:\!gajba\cdcovers\cdcover1.jpg';
  SaveJpegToTable(ADOTable1, ADOTable1Picture, SPicFileName);
}
var
  fS  : TFileStream;
begin
  fs:=TFileStream.Create(sPicPath, fmOpenRead);
  try
   Table.Edit;
   PicField.LoadFromStream(fs);
   Table.Post;
  finally
   fs.Free;
  end;
end;


end.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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