working with images in databases

Can anybody give we a nice and kozy way to add images to datebases from a delphi program , like with openimage dialog or something like that
hafhalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

men xinCEOCommented:
hafhai:

this code use table component.

var Stream: TBlobStream;
begin
  Table1.Append;
  Stream:=TBlobStream.Create(table1pic, bmWrite); //pic field is a blobfield
  Image1.Picture.Graphic.SaveToStream(Stream);//you can use another method,for example tbitmap.savetostream
  Table1.Post;
  Stream.Free;
  ...
end;
 

menxin

0
kretzschmarCommented:
hi hafhal,

i sample from my paq for storing jpg and gif images in a database (jpeg and gifunit are used in the uses-clause)


//Store image
procedure TForm1.Button1Click(Sender: TObject);
Var
  ExtS : String[4];  //Holds The Extension
begin
  if (Table1.State = dsEdit) or  (Table1.State = dsInsert) then    // Do only if in edit mode
  begin
    If openpicturedialog1.Execute then    // If file selected
    begin
      if Table1.FieldByName('Picture').IsBlob then   // this line can deleted
      begin  //remember the Extension
        ExtS := copy(AnsiUpperCase(ExtractFileExt(OpenPictureDialog1.FileName)),2,MaxLongInt);
        if ExtS = 'JPEG' then ExtS := 'JPG';  //Normalize Extension
        if (ExtS = 'JPG') or (ExtS = 'GIF') then  //If one of these extensions
        begin
          try
            TBlobField(Table1.FieldByName('Picture')).LoadFromFile(openpicturedialog1.filename);  //Store the File in the Blob
            Table1.FieldByName('Typ').AsString := ExtS; //Store the Extension in the Grafic-TypField
            image1.Picture.LoadFromFile(openpicturedialog1.filename); //Load Image, also from file
          except
            showMessage('Error during BlobOperation');
          end;
        end
        else showmessage('no jpg or gif-file selected');
      end;
    end;
  end else ShowMessage('Table is not in Edit Mode!');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Table1.Open;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Table1.Close;
end;

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
Var
  MyJpegImage : TJpegImage;
  MyGifImage : TGifFile;
  MyBlobStream : TBlobStream;
begin
  // Do Only if a Picture available
  if (Table1.FieldByName('Picture').IsBlob) and (not(Table1.FieldByName('Picture').IsNull)) then
  begin  //create a blobstream
    MyBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Picture')),bmRead);
    if Table1.FieldByName('Typ').Asstring = 'GIF' then  //if grafictyp = gif
    begin
      MyGifImage := TGifFile.Create;               //create GifObject, can also be done Global
      MyGifImage.LoadFromStream(MyBlobStream);     //get the information from the blob
      Image1.Picture.Assign(MyGifImage.AsBitmap);  //Display Gif
      MyGifImage.Free;                             //Free Object
    end
    else  //can only be JPG
    begin
      MyJPegImage := TJPegImage.Create;         //create JPegObject, can also be done global
      MyJPegImage.LoadFromStream(MyBlobStream); //get the information from the blob
      Image1.Picture.Assign(MyJPegImage);       //Display JPeg
      MyJPegImage.Free;                         //Free Object
    end;
    MyBlobStream.Free;  // Free Stream
  end
  else image1.Picture.assign(Nil);  // No Picture saved disable display
  screen.Cursor := crDefault;
  ProgressBar1.Position := 100;
end;

procedure TForm1.Table1BeforeScroll(DataSet: TDataSet);
begin
  screen.cursor := crhourglass;
  ProgressBar1.Position := 0;
end;

procedure TForm1.Image1Progress(Sender: TObject; Stage: TProgressStage;
  PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
  const Msg: String);
begin
  Progressbar1.Position := Percentdone;
end;

can be expanded for more pictureformatsupport

meikl
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
she3i3iCommented:
Listening...
0
hafhalAuthor Commented:
i bit whole lot more then i asked for , but then again , thats just better q;o)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.