[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

working with images in databases

Posted on 1999-12-05
4
Medium Priority
?
142 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:hafhal
4 Comments
 
LVL 1

Expert Comment

by:men xin
ID: 2257384
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 2257602
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
 
LVL 1

Expert Comment

by:she3i3i
ID: 2258461
Listening...
0
 

Author Comment

by:hafhal
ID: 2260373
i bit whole lot more then i asked for , but then again , thats just better q;o)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…

607 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