Jose Martinez
Jose Martinez

Reputation: 313

Reduce image quality in delphi

hello I'm trying to reduce the quality of a jpg image in delphi, the problem is that my code I get this error E2010 Incompatible types: 'TPersistent' and 'string'

My code is this:

   TForm1.Button4Click procedure (Sender: TObject);
   var
     imagen2: TJpegImage;
   begin

     image2: = TJpegImage.Create;
     imagen2.Assign ('c:/test.jpg');
     imagen2.CompressionQuality: = 60;
     imagen2.SaveToFile ('c:/test.jpg');

   end;

Someone can help me to correct the error?

Upvotes: 0

Views: 1005

Answers (2)

David Heffernan
David Heffernan

Reputation: 612884

Let's look at the error message:

Incompatible types: 'TPersistent' and 'string'

That is quite clear. The compiler expected TPersistent, but you passed string. The Assign method does indeed expect a TPersistent. To use assign you need to have two graphic objects. You don't have that. You've got a graphic object and a file name.

So Assign is not useful here. What you need to do is the inverse of SaveToFile which is LoadFromFile.

imagen2.LoadFromFile('c:\test.jpg'); 
imagen2.CompressionQuality := 60;
imagen2.SaveToFile ('c:\test.jpg');   

Don't be afraid of compiler errors. Read them, and try to work out what they mean.

Upvotes: 5

Ken White
Ken White

Reputation: 125671

Please learn to actually read the words in the error message. In this case, it's quite clear that you're trying to pass a string to something that expects a TPersistent. Examining the code that causes the error would make it very clear that TJpegImage.Assign is not expecting a filename, but something else.

The documentation makes it clear that to load something from a file to a TJpegImage you use LoadFromFile:

imagen2 := TJpegImage.Create;
imagen2.LoadFromFile('c:\test.jpg');

It also clearly documents what TJpegImage.Assign is expecting, and what it is intended to do (including two links to code examples, one in Delphi and one in C++).

procedure Assign(Source: TPersistent); override;

Copies the jpeg image object and creates a new reference tp the internal data source object.

Upvotes: 5

Related Questions