Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Need to save jpeg format stream


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 8 - Last Post: Jan 18, 2018 12:44 AM Last Post By: James L
James L

Posts: 33
Registered: 8/3/03
Need to save jpeg format stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 16, 2018 2:42 AM
Dear Sir,

In FMX, TImage->Bitmap->SaveToStream(AStream);
How to set this AStream is Jpeg format?

Thank you for help
James
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Need to save jpeg format stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 16, 2018 8:19 AM   in response to: James L in response to: James L
James L wrote:

In FMX, TImage->Bitmap->SaveToStream(AStream);
How to set this AStream is Jpeg format?

In FMX, TBitmap::SaveToStream() is hard-coded to PNG only, you can't
change that. You need to call TBitmapCodecManager::SaveToStream()
instead, which allows you to specify the type of the output via file
extension.

--
Remy Lebeau (TeamB)
James L

Posts: 33
Registered: 8/3/03
Re: Need to save jpeg format stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 16, 2018 9:00 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hello Remy,

FMX ImageView, its can save JPEG file to HDD,
My code like this
Image1->Bitmap->SaveToFile("MyTest.Jpg");
I don't like to create any files, just want to save jpeg file into stream for send.

thank you,

James
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Need to save jpeg format stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 16, 2018 9:39 AM   in response to: James L in response to: James L
James L wrote:

FMX ImageView, its can save JPEG file to HDD

TBitmap::SaveToFile() calls TBitmapCodecManager::SaveToFile() with a
filename. The file extension specifies the format to save in.

TBitmap::SaveToStream() calls TBitmapCodecManager::SaveToStream() with
a hard-coded ".png" file extension, so it can only save in PNG.

To do what you want, you need to call
TBitmapCodecManager::SaveToStream() directly, specifying ".jpg" as the
file extension (and optionally a pointer to a TBitmapCodecSaveParams to
specify the Quality of the output), eg:

TBitmapSurface *Surf = new TBitmapSurface;
try
{
    Surf->Assign(Image1->Bitmap);
 
    TBitmapCodecSaveParams params;
    params.Quality = ...;
 
    if (!TBitmapCodecManager::SaveToStream(Stream, Surf, L".jpg",
&params))
    {
        // handle error as needed...
    }
}
__finally
{
    delete Surf;
}


--
Remy Lebeau (TeamB)
James L

Posts: 33
Registered: 8/3/03
Re: Need to save jpeg format stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 16, 2018 5:51 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hello Remy,

Run app, get error stop here...

if (!TBitmapCodecManager::SaveToStream(JpegStream, Surf, L".jpg", &params))
{
// handle error as needed...
}

Error Message:
'TStream.Seek not implemented'

Edited by: James L on Jan 16, 2018 5:51 PM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Need to save jpeg format stream [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 17, 2018 9:37 AM   in response to: James L in response to: James L
James L wrote:

Error Message:
'TStream.Seek not implemented'

That error is thrown by the 32-bit version of TStream::Seek() if the
64-bit version is not overriden in a derived class. Typically, you
would see this error only if you instantiate an instance of TStream
itself, which is impossible in C++ (but possible in Delphi) since
TStream is an abstract class. What is your JpegStream declared as, and
how is it being instantiated before calling SaveToStream()?

--
Remy Lebeau (TeamB)
James L

Posts: 33
Registered: 8/3/03
Re: Need to save jpeg format stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 16, 2018 6:58 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
TBitmapSurface *Surf = new TBitmapSurface;
try
{
    Surf->Assign(Image1->Bitmap);
 
    TBitmapCodecSaveParams params;
    params.Quality = ...;
 
    if (!TBitmapCodecManager::SaveToStream(Stream, Surf, L".jpg",
&params))
    {
        // handle error as needed...
    }
}
__finally
{
    delete Surf;
}


--
Remy Lebeau (TeamB)

It's work, thank you,
below is code:

TBitmapCodecManager::SaveToStream(JpegStream, Surf, NULL, &params);
// why can not load JpegStream to Image bitmap?
Image1->Bitmap->LoadFromStream(JpegStream);
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Need to save jpeg format stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 17, 2018 9:39 AM   in response to: James L in response to: James L
James L wrote:

TBitmapCodecManager::SaveToStream(JpegStream, Surf, NULL, &params);

Why are you passing NULL for the file extension? TBitmapCodecManager
supports multiple image types, so you have to pass ".jpg" to tell it
you want a JPEG as output.

// why can not load JpegStream to Image bitmap?
Image1->Bitmap->LoadFromStream(JpegStream);

Make sure you seek the stream's Position back to 0 before calling
LoadFromStream().

--
Remy Lebeau (TeamB)
James L

Posts: 33
Registered: 8/3/03
Re: Need to save jpeg format stream  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 18, 2018 12:44 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
It's work, need add L".jpg"
Thank you,
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02