Using TAdvPicture to convert to a valid jpeg

Hello

In Delphi 10.4, VCL :
Is it possible to use the TAdvPicture to convert an image to a valid JPEG ?
I have a picture which raises an error :"jpeg error #53" when I try to assign it to a bitmap, jpeg, image.
But, the TAdvPicture-component shows it without any problem.

So, this works :

AdvPicture1.Picture.LoadFromFile( 'bloem.jpg' ) ;

But I cannot save/convert the image to a valid JPEG

This is what I raises the error :

procedure TForm1.Button3Click(Sender: TObject);
var
Graphic : TGraphic ;
vPic : TPicture;
begin

Graphic := TJPEGImage.Create ;
Graphic.LoadFromFile('bloem.jpg' );
// ----> ... raised exception class EJPEG with message 'JPEG error #53'
end;

This also rasises the same error:
procedure TForm1.Button1Click(Sender: TObject);
var
JPEG : TJPEGImage ;
begin
JPEG := TJPEGImage.Create ;
JPEG.LoadFromFile( 'bloem.jpg' ) ;
// --> raised exception class EJPEG with message 'JPEG error #53'*

I tried to save the loadedimage to a file first :

AdvPicture1.Picture.SaveToFile('advPicture.jpeg') ;

but without any result ..

Finaly, I managed to show the picture in a TImage as follows :

procedure TForm1.bbAdv2JpegClick(Sender: TObject);

var
JPEG : TJPEGImage ;
memStream : TMemoryStream ;
bm: TBitMap ;
begin

JPEG := TJPEGImage.Create ;
memStream :=TMemoryStream.Create ;
memStream.Position := 0 ;

AdvPicture1.Picture.LoadFromFile( 'bloem.jpg' ) ;
AdvPicture1.Picture.SaveToStream( memStream ) ;
memStream.Position := 0 ;

bm := VCL.Graphics.TBitmap.Create ;
bm.LoadFromStream( memstream ) ;
image1.Picture.Bitmap.Assign( bm ) ;
image1.Picture.SaveToFile('Adv2JPEG.JPEG') ;
RotateImage1.Picture.LoadFromFile('Adv2JPEG.JPEG') ;
// --> raised exception class EJPEG with message 'JPEG error #53'*

I searched for "TAdvPicture" but did not get any results...

any hints are welcome !

kind regards,

Dirk Janssens.

If there is something in the JPEG that triggers an error in the VCL JPEG component, it won't help by just assigning the same data to the VCL class.
A possible solution is to draw the TAdvPicture on a TBitmap class and then save this bitmap, i.e.

bitmap.canvas.Draw(0,0,AdvPicture.Picture);

Thank you, but i must be doing something wrong. everything I tried generates an empty file..

procedure TForm1.Button4Click(Sender: TObject);
var
bm: TBitMap ;
begin

AdvPicture1.Picture.LoadFromFile( 'bloem.jpg' ) ; // picture is visible in the component on the form

bm := VCL.Graphics.TBitmap.Create ;
bm.Canvas.Draw(0,0, AdvPicture1.Picture ) ;

bm.SaveToFile('JPG2BM.bmp') ;
---> file is empty

image1.Picture.Bitmap.Assign( bm ) ; // image1 is a TImage-component on the form
---> picture is not shown*

image1.Picture.Bitmap.SaveToFile('BitmpToJpeg.bmp') ;
---> File is empty,*

//direct in TImage :
Image1.Picture.Bitmap.Canvas.Draw(0,0 , AdvPicture1.Picture) ;
image1.Picture.Bitmap.SaveToFile('BitmpToJpeg2.bmp') ;
// ---> File is empty, picture is not showing

end;

What am I missing ?

Set the bitmap width / height

bm := VCL.Graphics.TBitmap.Create ;
bm.Width := ...picture width
bm.Height := ..picture height
bm.Canvas.Draw(0,0, AdvPicture1.Picture ) ;

Thank you,
Unfortunatly, the JPEG-error is still there (!)

bm := VCL.Graphics.TBitmap.Create ;
bm.Width :=AdvPicture1.Picture.Width ;
bm.Height :=AdvPicture1.Picture.Height ;

bm.Canvas.Draw(0,0, AdvPicture1.Picture ) ;
bm.SaveToFile('aJPG2BM.bmp') ;
bm.SaveToFile('aJPG2BM.JPEG') ;
bm.SaveToFile('aJPG2BM.PNG') ;

There is no difference between the new files and the original one...

I cannot see any problem. It works fine here. See project.Project1.zip (218.3 KB)

Hi Bruno,

Thank you, the project works, but this does not answer my question in how to covert the picture to a JPEG. ( Your project generates a bitmap )
The problem is: I have a picture ( bloem.jpg ) which somehow generates a JPEG-error. Because the picture loads in the TAdvPicture-component, I am looking for a way to save it as a ( valid) JPEG.

Let me explain the background :
I have a program in which the user can upload jpeg to a 'image bank' (beeldbank FloraHolland) with a webservice.
There are 400 users, they all make their own pictures.
It sometimes happens that a picture is not accepted. The "bloem.jpg" is one of them. I have no control over how the pictures are made.
The same picture "bloem.jpg" however can be uploaded on the site of FloraHolland.
They tell me that they convert the picture to a valid format before putting it in the image bank.
Because I want my users to do the same thing in my application, I am looking for a robust way to genereate valid JPEG files.

kind regards,

Dirk

I explained that the way to do this is going via TAdvPicture drawing
I showed you how you can draw it. It is then up to your application level to choose the format to save it to file. This is outside the scope of our component and our support.

To convert a bitmap to JPEG, see:
http://www.scalabium.com/faq/dct0042.htm