JPG in Databases

Goran Brumen delphi-db@elists.org
Thu, 20 Jun 2002 08:47:36 +0200


Hi Fabio,
maybe this two functions could help You; they perfectly do in Paradox =
tables but they should work also on every other database that supports =
binary BLOBs:


function SaveJPEG(dsDestination : TDataSet; Img : TImage; Fld : String) =
: Integer;
var
	JPEG : TJpegImage;
   BLStream : TBlobStream;
begin
   Result :=3D -1;
   Jpeg :=3D TJpegImage.Create;
   try
      //Every image should be converted as JPEG
      Jpeg.CompressionQuality :=3D 75;
      Jpeg.Assign(Img.Picture.Graphic);
      BlStream :=3D TBlobStream.Create(dsDestination.FieldByName(Fld) as =
TBLOBField, bmReadWrite);
      Try
         //Saving image...
         BLStream.Position :=3D0;
         Jpeg.SaveToStream(blStream);
         Result :=3D 1;
      finally
         BLStream.Free;
     end;
   finally
      Jpeg.Free;
   end;
end;

function GetJPEG(dsSource : TDataSet; Field : String; var Img : TImage) =
: Integer;
var
   JPEG : TJpegImage;
   BLStream : TBlobStream;
begin
   Result :=3D -1;
   Jpeg :=3D TJPEGImage.Create;
   try
      //If field is not empty, we can proceed with JPEG retrieve
      if not dsIzvor.FieldByName(Polje).isNull then begin
         BLStream :=3D TBLOBStream.Create(dsSource.FieldByName(Fld) as =
TBLOBField, bmRead);
         try
            BLStream.Position :=3D 0;
            try
	    Jpeg.LoadFromStream(BLStream);
   	    Img.Picture.Assign(JPEG);
	    Result :=3D 1;
            except
            end;
         finally
            BLStream.Free;
         end;
      end; //if not isNull
   finally
      Jpeg.Free;
   end;
end;

What You have to do is to pass to the function datasource for In/Out =
procedure, an object of TImage type and the field name. Then the =
functions will save or retrieve any picture of TImage type as JPEG. I =
have choosen to save pictures as JPEG's as they are small. As far as I =
know TDBImage can work only with BMP, maybe also PCX. Every other format =
should be converted into BMP.

Hope this will help You!

Best regards
Goran Brumen

> -----Original Message-----
> From: Fabiano Paolini - personale [mailto:FPaolini@iol.it]
> Sent: Thursday, June 20, 2002 8:16 AM
> To: delphi-db@elists.org
> Subject: Re: JPG in Databases
>=20
>=20
> OK, thanks.
> But cannot use DBImages?
>=20
> Now my project is working using TImage and loading / saving=20
> images from a
> BLOB field manually.
> Problem: my need is to save any kind of image in the BLOB (it=20
> has to work
> with BMP, JPG, ICO...).
> Solution: I've saved also the type of the image in another=20
> field of the
> Database (the file extension). The only way I can load BLOB=20
> in TImage is
> saving Blob in a temporary file and reloading it in TImage!=20
> How can do the
> same using streams? (TImage.Picture allows only LoadFromFile and not
> LoadFromStream).
>=20
> Fabiano
>=20
> ----- Original Message -----
> From: "Liviu" <liviu@audicon.ro>
> To: <delphi-db@elists.org>
> Sent: Wednesday, June 19, 2002 2:26 PM
> Subject: Re: JPG in Databases
>=20
>=20
> > > I've made a BLOB field in Database (Oracle 8.xx via DOA)=20
> to contain
> > images.
> > > I can view if ok and can load it using
> > >
> > > Dataset.Edit;
> > > DBImage.LoadFromFile(..):
> > > DataSet.Post;
> > >
> > > and all works fine with BMPs.
> > >
> > > Trying to use JPGs, i can do LoadFromFile and I can see=20
> correct image in
> > the
> > > DBImage, but after the Post, the BLOB field results empty!
> > >
> > > Can anybody help me?
> > >
> > Hi, Fabiano
> >
> > Here is a piece of code I used for inserting BMP's/JPEG's:
> >
> > procedure WriteImageInDataBase(img : TImage);
> > var
> >   Jpg: TJpegImage;
> >   S: TMemoryStream;
> > begin
> >     ibtbAlbum.Insert;
> >     Jpg :=3D TJpegImage.Create;
> >     if img.Picture.Graphic is TBitmap then begin
> >       Jpg.Assign(img.Picture.Bitmap); end
> >     else {img.Picture.Graphic is TJPEGImage} begin
> >       Jpg.Assign(img.Picture.Graphic);
> >     end;
> >     S :=3D TMemoryStream.Create;
> >     Jpg.SaveToStream(S);
> >     ibtbAlbumIMAGINE.LoadFromStream(S);
> >     ibtbAlbum.Post;
> > end;
> >
> > You must have the jpeg unit in the uses clause.
> > I hope it will help
> >
> >
> >
> >
> > _______________________________________________
> > Delphi-DB mailing list
> > Delphi-DB@elists.org
> > http://www.elists.org/mailman/listinfo/delphi-db
>=20
>=20
> _______________________________________________
> Delphi-DB mailing list
> Delphi-DB@elists.org
> http://www.elists.org/mailman/listinfo/delphi-db
>=20