Board index » delphi » Converting JPEG to a string and vice versa

Converting JPEG to a string and vice versa

Is it possible to convert a JPEG image to a string?  Can that string then be
converted back to JPEG?  I would like to store pictures in a database table.
One way to do this would be to store the pictures as a BLOB.

Any help would be appreciated.

Thanks,
Brad

 

Re:Converting JPEG to a string and vice versa


Quote
> Is it possible to convert a JPEG image to a string?

If you really want to, you can convert anything to a string. Just write it
as Base64, either standard or your own algorythm:

var
  b: Byte;
  f: file of byte;
  s:string;
begin
s := '';
Assignefile(f, 'c:\my.jpg');
While not EOF(f) do
  Read(f, b);
  s := s + IntToHex(b, 2);
end;
closefile(f);

The above is just an example and it expands the file to double size.

--
Andrei "Ndi" Dobrin
Brainbench MVP
www.Brainbench.com

Re:Converting JPEG to a string and vice versa


Quote
Ndi wrote:

> > Is it possible to convert a JPEG image to a string?

> If you really want to, you can convert anything to a string. Just write it
> as Base64, either standard or your own algorythm:

> var
>   b: Byte;
>   f: file of byte;
>   s:string;
> begin
> s := '';
> Assignefile(f, 'c:\my.jpg');
> While not EOF(f) do
>   Read(f, b);
>   s := s + IntToHex(b, 2);
> end;
> closefile(f);

> The above is just an example and it expands the file to double size.

never,
never do it this way.
Since the string s grows in size, you have a considerable slowdown
due to the frequent reallocation and copying. Set the stringsize first,
then fill it.

Rene

Re:Converting JPEG to a string and vice versa


On Wed, 27 Nov 2002 15:24:09 -0500, "Brad Huff" <bhuf...@hotmail.com>
wrote:

Quote
>Is it possible to convert a JPEG image to a string?  Can that string then be
>converted back to JPEG?  

Yes, sure, but....

Quote
>I would like to store pictures in a database table.
>One way to do this would be to store the pictures as a BLOB.

there's no need to convert it into a string -- BLOB fields can hold
the JPEG just as it is.  TJPEGStream will write into  TBLOBStream
quite nicely.

Nick Hodges
Lemanix Corporation
How to ask questions of techies --
http://www.tuxedo.org/~esr/faqs/smart-questions.html

Re:Converting JPEG to a string and vice versa


Quote
> never,
> never do it this way.
> Since the string s grows in size, you have a considerable slowdown
> due to the frequent reallocation and copying. Set the stringsize first,
> then fill it.

> Rene

It was pointer on conversion, not optimization :)

Ndi

Re:Converting JPEG to a string and vice versa


Quote
> I just wanted to correct a possible negative experience.
> The loss of speed is not immediately visible to the
> unprimed.

The speed penalty is huge on large files. He'll catch on :)

Re:Converting JPEG to a string and vice versa


Quote
Ndi wrote:

> > I just wanted to correct a possible negative experience.
> > The loss of speed is not immediately visible to the
> > unprimed.

> The speed penalty is huge on large files. He'll catch on :)

Sure. I meant the origin of the speed penalty was not
immediately visible.

Rene

Re:Converting JPEG to a string and vice versa


On Fri, 29 Nov 2002 08:17:38 -0500, "Brad Huff" <bhuf...@hotmail.com>
wrote:

Quote
>Can you provide a code snippet showing how a .JPEG file can be stored into a
>database field?  Also, show how it can be retrieved and displayed into
>TImage.picture.  I am sure that it is quite simple.

Off the top of my head --

procedure TForm1.Button1Click(Sender: TObject);
var
  BlobStream: TBlobStream;
  JPEGImage: TJPEGImage;
begin
  JPEGImage := TJPEGImage.Create;
  try
    BlobStream := MyDataset.CreateBlobStream(MyFieldWithAJPEGInIt,
bmRead);
    try
      BlobStream.Position := 0;
      JPEGImage.LoadFromStream(BlobStream);
      Image1.Picture.Assign(JPEGImage);
    finally
      BlobStream.Free;
    end;
  finally
    JPEGImage.Free;
  end;
end;

Do the reverse for storing into the BlobStream.  

Here's a routine you might like as well.

procedure CopyStreamToStream(FromStream, ToStream: TStream);
begin
  if (FromStream <> nil) and (ToStream <> nil) then
  begin
    FromStream.Position := 0;
    ToStream.Position := 0;
    ToStream.CopyFrom(FromStream, FromStream.Size);
  end;
end;

Re:Converting JPEG to a string and vice versa


"Nick Hodges (TeamB)" <nickhod...@yahoo.com> wrote in message
news:26vhuu478p22487maan0i5351h8b805fe5@4ax.com...

Quote
> On Fri, 29 Nov 2002 08:17:38 -0500, "Brad Huff" <bhuf...@hotmail.com>
> wrote:

> >Can you provide a code snippet showing how a .JPEG file can be stored
into a
> >database field?

Just my 2 cents:

I find with IBExpress (with Interbase 6.x) I allways get JPEG errors with :
    BlobStream := MyDataset.CreateBlobStream(MyFieldWithAJPEGInIt, bmRead);

Then your code as before.

But not with:
   StringStream :=
TStringStream.Create(IBDataSet.FieldByName('PHOTO').AsString);

Then your code as before.

Not sure why.

Kevin

Re:Converting JPEG to a string and vice versa


you could always MimeEncode it, like email does.

Quote
"Kevin S." <m...@ecostatsnono.com> wrote in message

news:3dea364b@newsgroups.borland.com...
Quote
> "Nick Hodges (TeamB)" <nickhod...@yahoo.com> wrote in message
> news:26vhuu478p22487maan0i5351h8b805fe5@4ax.com...
> > On Fri, 29 Nov 2002 08:17:38 -0500, "Brad Huff" <bhuf...@hotmail.com>
> > wrote:

> > >Can you provide a code snippet showing how a .JPEG file can be stored
> into a
> > >database field?

> Just my 2 cents:

> I find with IBExpress (with Interbase 6.x) I allways get JPEG errors with
:
>     BlobStream := MyDataset.CreateBlobStream(MyFieldWithAJPEGInIt,
bmRead);

> Then your code as before.

> But not with:
>    StringStream :=
> TStringStream.Create(IBDataSet.FieldByName('PHOTO').AsString);

> Then your code as before.

> Not sure why.

> Kevin

Other Threads